How to obtain width and height of image after rotating it? - php

How can I get the width and height of the image after rotating it using imagerotate() in PHP?
Here is my code:
<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
But what I want to do before doing the output, is that I want to get the width and height of the rotated image. How can I do that?

I'm sure you can do something similar to this:
$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];
Another option is this:
list($width, $height) = getimagesize($filename);

imagerotate returns an image ressource. Hence you cannot use getimagesize which works with an image file.
Use
$width = imagesx($rotate);
$height = imagesy($rotate);
instead.

Related

Photo has wrong colors after resized with PHP script

I'm using the following PHP function to resize big images to fit 500 px width:
<?php
function resizeImage($name) {
header('Content-type: image/jpeg');
$filename = "file.jpg";
$new_width = 500;
list($width, $height) = getimagesize($filename);
$new_height = (($height*$new_width)/$width);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, "file.jpg", 100);
}
?>
For some reason the colors on the resized image aren't exactly the same as before. They aren't as clear and strong as before. As you can see [picture removed] there's more red color and brilliance in the left (original) photo.
Why that? Is there something wrong with my script? Or is it a normal resizing effect?
This is the working code I have now:
<?php
// Call the function with: resizeImage("INSERT_YOUR_FILE_NAME_INCLUDING_SUFFIX_HERE");
function resizeImage($file_name) {
// File is located at: files/original/
$filename = "files/original/".$file_name;
// The width you want the converted image has
$new_width = 500;
// Calculate right height
list($width, $height) = getimagesize($filename);
$new_height = (($height*$new_width)/$width);
// Get image
$small = new Imagick($filename);
// Resize image, but only if original image is wider what the wanted 500 px
if($width > $new_width) {$small->resizeImage($new_width, $new_height, Imagick::FILTER_LANCZOS, 1);}
// Some code to correct the color profile
$version = $small->getVersion();
$profile = "sRGB_IEC61966-2-1_no_black_scaling.icc";
if((is_array($version) === true) && (array_key_exists("versionString", $version) === true)) {$version = preg_replace("~ImageMagick ([^-]*).*~", "$1", $version["versionString"]);if(is_file(sprintf("/usr/share/ImageMagick-%s/config/sRGB.icm", $version)) === true) {$profile = sprintf("/usr/share/ImageMagick-%s/config/sRGB.icm", $version);}}if(($srgb = file_get_contents($profile)) !== false){$small->profileImage("icc", $srgb);$small->setImageColorSpace(Imagick::COLORSPACE_SRGB);}
// Safe the image to: files/small/
$small->writeImage("files/small/".$file_name);
// Clear all resources associated to the Imagick object
$small->clear();
}
?>
Don't forget to either download the icc file from http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc and save it in the same directory as your resize file or change $profile = "sRGB_IEC61966-2-1_no_black_scaling.icc"; to $profile = "http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc";!

Resizing image using php

I am trying to resize my image by making it 200 x 200 while keeping the aspect ratio. I am trying to follow http://php.net/manual/en/function.imagecopyresampled.php to try to accomplish this. Here is my code:
<?php
$directory = "uploads/";
$images = glob($directory."*.jpeg");
foreach($images as $filename) {
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
}
?>
I took out the content type because it was giving me an error. Instead of outputting the resized image, it was outputting bunch of symbols like this:
JFIF�����t��jw��E��4."�DN�9>m���4����P};WI��x"�.
Anybody know why this is happening?
You need to sent header to a browser so your output could be properly understand by browser. Add following line to your script:
header('Content-Type: image/jpeg');
You need to have the header('Content-Type: image/jpeg'); before the foreach loop
This code must be at the very beginning of your file:
header('Content-Type: image/jpeg');
And the above code should be standalone, you shouldn't have any HTML tags anywhere and it shouldn't be "PHP-included" anywhere.

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

Image not rotating

I am trying to rotate image using php imagerotate function but its not working.
GD Library is also on.
i have tried this ,
public function rotate()
{
$targ_w = 240;
$targ_h = 180;
$jpeg_quality = 100;
$degrees = 90;
$src = "/photos/sunset.jpg";
$image = imagecreatefromjpeg($src);
$rotatedImage = imagerotate($image,$degrees,0);
imagejpeg( $rotatedImage,$src,$jpeg_quality);
imagedestroy($rotatedImage);
die();
}
<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
You're outputing the unchanged $image to file. You should output the rotated one.
imagejpeg( $rotatedImage,$name ,$jpeg_quality);
The second thing - your image is empty. It has only defined width and height but has no content inside it. You defined a $src variable but you don't use it at all.
Maybe you want to replace imagecreatetruecolor with this:
$src = "/photos/sunset.jpg";
$image = imagecreatefromjpeg($src);
You must output the rotated image (pass $rotatedImage instead of $image):
$rotatedImage = imagerotate($image,$degrees,0);
header('Content-type: image/jpeg'); //Header is required to output the image.
imagejpeg($rotatedImage,$name ,$jpeg_quality);
imagedestroy($rotatedImage);
die();
If you are trying to show the image then you need to change that:
header('Content-type: image/jpeg'); //Add jpeg header
imagejpeg( $rotatedImage, NULL, 100); //<-- Notice i remove the $src parameter
If you want to update your jpg file then your code will work, but the user that runs the php file need permissions to write the file. Of course your current image will be overwritten.
And as i said in comments you will need GD version 1.8 or later to work with jpeg files according to php.net

PHP GD - Watermark image with opacity

I am trying to add a watermark to an image using PHP and the GD image library. I can apply the watermark where I specify with the correct opacity setting.
The problem is that my watermark itself has a transparent background. When I try to apply this watermark to the image I get a black background.
The image which the watermark is being applied to is a jpeg. Could this be the problem? If so how would I convert the jpeg into a format which supports transparency, apply watermark, then convert it back?
This is the key bit of code I have at the moment.
// Determine image size and type
$size = getimagesize($this->image_path);
$size_x = $size[0];
$size_y = $size[1];
$image_type = $size[2]; // This is always a JPEG
// load source image
$image = $this->ImageCreateFromType($image_type, $this->image_path);
// Determine watermark size and type
$wsize = getimagesize($watermark_path);
$watermark_x = $wsize[0];
$watermark_y = $wsize[1];
$watermark_type = $wsize[2]; // This is typically a PNG
// load watermark
$watermark = $this->ImageCreateFromType($watermark_type, $watermark_path);
$dest_x = $this->setX($size_x, $watermark_x);
$dest_y = $this->setY($size_y, $watermark_y);
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, $this->opacity);
While not really relevant, here is the code for the ImageCreateFromType function
function ImageCreateFromType($type,$filename) {
$im = null;
switch ($type) {
case 1:
$im = ImageCreateFromGif($filename);
break;
case 2:
$im = ImageCreateFromJpeg($filename);
break;
case 3:
$im = ImageCreateFromPNG($filename);
imagealphablending($im, true);
imagesavealpha($im, true);
break;
}
return $im;
}
Have a read about the imagecolortransparent() function: http://php.net/manual/en/function.imagecolortransparent.php
You may also want to look at this question: Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

Categories