Reputation: 375
In php I have uploaded an image to the database by using php move_uploaded_file function. Now when I am getting the image from the database I am using this code to get the image
$result = mysql_query("SELECT * FROM "._DB_PREFIX_."storeimages WHERE `city_name`='".$_GET['details']."'");
while($row = mysql_fetch_array($result)){
echo '<div class="store-img">';
echo '<img class="store-image" src="storeimages/images/'.$row['store_image'].'" width="100px" height="100px" >';
echo '</div>';
}
Here I am getting the image easily. But here you can see I have used width="100px"
and height="100px"
for image size. This is disturbing the image aspect ratio. To solve this I have searched over google and I got that imagemagick is a good option for that.But I don't know how to use imagemagick with simple php (I am not using any class, method) and how can I use imagemagick here? Any help and suggestions will be really appreciable. Thanks
Upvotes: 3
Views: 1257
Reputation: 51
install php imagick
sudo apt-get install imagemagick
sudo apt-get install php5-imagick
While resizing the photo is better to maintain the aspect ratio of the photo. Following code should give a better idea of how to calculate the aspect ratio
if( $imageWidth > $maxWidth OR $imageHeight > $maxHeight )
{
$widthRatio = 0;
$heightRatio = 0;
if( $imageWidth > 0 )
{
$widthRatio = $maxWidth/$imageWidth;
}
if( $imageHeight > 0 )
{
$heightRatio = $maxHeight/$imageHeight;
}
if( $widthRatio > $heightRatio )
{
$resizeRatio = $heightRatio;
}
else
{
$resizeRatio = $widthRatio;
}
$newWidth = intval( $imageWidth * $resizeRatio );
$newHeight = intval( $imageHeight * $resizeRatio );
}
Refer http://php.net/manual/en/book.imagick.php for how to use Imagick. You can refer the following sample code
$image = new Imagick($pathToImage);
$image->thumbnailImage($newWidth, $newHeight);
$image->writeImage($pathToNewImage);
Upvotes: 0
Reputation: 700
imagemagick is a linux utility through which you can manipulate images
In order to use , you must have it installed on your server
just type following command
<?
print_r(exec("which convert"));
?>
if it return something then it is installed
Now use following command to resize image
<?php
exec("/<linux path of this utility>/convert /<actual path of image>/a.png -resize 200x200 /<path where image to be saved>/a200x200.png")
?>
Upvotes: 0
Reputation: 2289
Here is how to maintain image ratio
list($origWidth, $origHeight) = @getimagesize("path/to/image");
$origRatio = $origWidth/$origHeight;
$resultWidth = 100;
$resultHeight = 100;
if ( $resultWidth/$resultHeight > $origRatio ) {
$resultWidth = $resultHeight * $origRatio;
} else {
$resultHeight = $resultWidth / $origRatio;
}
Upvotes: 1
Reputation: 1487
Try Sencha .io . Is very easy and powerful
echo '<img
src="http://src.sencha.io/100/http://yourdomain.com/storeimages/images/'.$row['store_image'].'"
alt="My constrained image"
width="100"
height="100"
/>';
More information there: http://www.sencha.com/learn/how-to-use-src-sencha-io/
Upvotes: 0