PHP image dimension - php

How to get width and height of an image?
I have $variable = 'http://site.com/image.png"
Want to get width of this image to $width and height to $height.
Like:
$variable = 'http://site.com/image.png"
$width = '300'; // image width is 300px
$height = '500'; // height is 500px
Thanks.

$variable = 'http://site.com/image.png';
$image = getimagesize($variable);
$width = $image[0];
$height = $image[1];
$type = $image[2];

getimagesize function. Maybe it will help you.

Related

get Width and height of image php from imageCreateFromBMP($src)

I want to know the image width and height of an image from the return image using imageCreateFromBMP($src) rather than $src. How can i achieve this ?
$src = 'four.bmp'; //src of image
$im = imagecreatefrombmp($src);
list($width, $height) = GetImageSize($im); // it doesnot work though it takes string
echo 'Image width '.$width.'</br>';
echo 'Image height '.$height.'</br>';
imagesx() and imagesy() take an image resource
http://php.net/manual/en/function.imagesx.php
http://php.net/manual/en/function.imagesy.php
Example:
$src = 'four.bmp'; //src of image
$im = imagecreatefrombmp($src);
$width = imagesx($im);
$height = imagesy($im);
echo 'Image width '.$width.'</br>';
echo 'Image height '.$height.'</br>';

Scale down an image to a fixed width and Height with same aspect resolution

I am trying to scale down an uploaded image to a fixed with and height for example: width=200px and height 200px. I tried to scale down the width of the image with a fixed width and calculate the new height by its new width. What now i am trying to achieve is to scale down both width and height to a fixed size width=200px & height =200px
My HTML:
<form action="do_upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload_image">
<br/>
<br/>
<input type="submit" value="submit">
</form>
do_upload.php:
<?php
move_uploaded_file($_FILES["upload_image"]["tmp_name"], "uploads/" . $_FILES["upload_image"]["name"]);
$image_path = "uploads/" . $_FILES["upload_image"]["name"];
$src = imagecreatefromjpeg($image_path);
list($width, $height) = getimagesize($image_path);
$newwidth = 200;
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0,0,0,$newwidth,$newheight, $width, $height);
imagejpeg($tmp, "uploads/small.jpeg", 100);
imagedestroy($src);
imagedestroy($tmp);
?>
This is working fine for me and scaling down the image. Kindly guide me how can i scale down all image (jpeg,png,gif etc) to a fixed width and height. Just guide me in the direction i will do it. If anyone can explain with an example or edit my code for me it will be great. Thanks in advance
You can see comment above to answer question about what to do with dimension that is less than 200 and aspect ration. That being said, I can go ahead and answer the question on how to scale on either dimension. Basically the problem is pretty simple.
First you determine if existing image has width or height which is greater dimension. Based on that, you determine the scaling factor for the image and then apply that scaling factor to both dimensions.
In code that might look like:
$src = imagecreatefromjpeg($image_path);
list($width, $height) = getimagesize($image_path);
$max_dimension = 200;
if($width >= $height) {
// scale on width
$scaling_factor = $width / $max_dimension;
} else {
// scale on height
$scaling_factor = $height / $max_dimension;
}
$new_width = $width / $scaling_factor;
$new_height = $height / $scaling_factor;
A more generalized solution which would let you specify any rectangle (i.e. different max height and width) within which to scale might look like this.
$max_width = 200;
$max_height = 150;
// determine which dimension must be scaled to fit target size
$width_scaling_factor = $width / $max_width;
$height_scaling_factor = $height / $max_height;
if($width_scaling_factor >= $height_scaling_factor) {
$scaling_factor = $width_scaling_factor;
} else {
$scaling_factor = $height_scaling factor;
}
$new_width = $width / $scaling_factor;
$new_height = $height / $scaling_factor;

Get a image height according to width using php

I want to get a image size according to the width and also I'm resizing the width if more then 500px
Here is my code so far
list($width, $height) = getimagesize("MyImage.jpg");
if ($width<300){
$NewWidth = $width;
}else{
$NewWidth = '500';
}
Just like to know how to get new height according to the width. Really appreciate your help or any advice you can give.
You need to get current image scale and then use that to calculate the new height from the new width. Divide the width by the height to get the scale. Then divide the new width by the scale to get the new (proportional) height.
list($width, $height) = getimagesize("MyImage.jpg");
// Get the image scale.
$scale = $width / $height;
// Existing width modification.
if ($width<300){
$NewWidth = $width;
}else{
$NewWidth = '500';
}
// Get new height from new width.
$NewHeight = $NewWidth / $scale;
Try this formula to maintain 4:3 aspect ratio.
new height = ((width X 3) / 4)

How to get image size from a canvas?

Is it possible to get the image size in pixels from a canvas?
E.g. I know I can achieve it getting the size from the image directly:
list($width, $height) = #getimagesize($_FILES['inputFieldName']['tmp_name'])
but I want to get it from a canvas. E.g.:
$canvas = imagecreatefromjpeg($image_path);
//Get image size from $canvas
Try:
$canvas = imagecreatefromjpeg($image_path);
$width = imagesx($canvas);
$height = imagesy($canvas);
Details at http://es1.php.net/manual/en/function.imagesx.php and http://es1.php.net/manual/en/function.imagesy.php

php thumbnail - creates a grainy back and white image

My code below creates the thumb but it is pretty grainy and although not black and white looks pretty washed out.
Any ideas how I can create a better quality thumb?
$thumb = $targetPath."Thumbs/".$fileName;
$image = imagecreatefromjpeg($targetFile);
$width = 200; //New width of image
$height = 92; //This maintains proportions
$thumbWidth = 200;
$thumbHeight = 92;
$sourceImage = imagecreatefromjpeg($targetFile);
$targetImage = imagecreate($thumbWidth,$thumbHeight);
imagecopyresampled($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));
//imagejpeg($targetImage, "$thumbPath/$thumbName");
imagejpeg($targetImage, $thumb, 100);
chmod($thumb, 0755)
;
thanks R.
use imagick, it will give way better results http://lt.php.net/manual/en/function.imagick-thumbnailimage.php
Use this: ini_set("gd.jpeg_ignore_warning", 1);

Categories