Imagemagick does not set image to exact sizes - php

I have an image that i want to set to 1024 x 768 and this is the code i am using
<?php
function autoRotateImage($image) {
$orientation = $image->getImageOrientation();
switch($orientation) {
case imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateimage("#000", 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_RIGHTTOP:
$image->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
// Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image!
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
}
$image = 'C:\xampp\htdocs\uncompressed_images\20221016_120159.jpg';
// Create new Imagick Object
$imagick = new Imagick($image);
// Set the Compression to COMPRESSION_JPEG
$imagick->setImageCompression(imagick::COMPRESSION_JPEG);
// Set the Compression quality
// This is where that compression method imagick::COMPRESSION_JPEG is
// used in the program.
$imagick->setImageCompressionQuality(26);
$imagick->thumbnailImage(1024,768);
autoRotateImage($imagick);
// Show the output
$imagick->setformat('jpg');
$imagick->writeImage($image);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
?>
However the output image is always 768 x 1024 How can i have the resulting to be 1024 x 768?
How can i also write image to a specific directory in this line $imagick->writeImage($image); i.e output directory

You should use autoRotateImage to fix any orientation issue first (so now the image is in right orientation) , and then apply thumbnailImage for resizing
So change
$imagick->thumbnailImage(1024,768);
autoRotateImage($imagick);
to
autoRotateImage($imagick);
$imagick->thumbnailImage(1024,768);

Related

Image is rotated 90 degree when displayed (image captured by a smartphone)

Hi I am working on a php site where user can use PHP to upload an image and then the system will display the image , resized (to a smaller size). The resize codes are as follows:
<?php
ini_set('memory_limit', -1);
ini_set('max_execution_time', 40000);
require_once 'ThumbLib.inc.php';
$fileName = (isset($_GET['file'])) ? urldecode($_GET['file']) : null;
$thumb = PhpThumbFactory::create($fileName);
$thumb->Resize($_GET['width'], $_GET['height']);
$thumb->show();
?>
where the html codes are
<img src="show_image.php?width=230&height=1000000&file=appsub/<?php echo $v["xfile"]; ?>">
There is nothing wrong if the user uploads the image thru a PC, but when the user captures a photo using a smartphone (e.g. iPhone), sometimes the image displayed will be rotated by 90 degree .
How can I fix the problem ?
if you are not saving the rotated image, you may use the following to display it (after rotation)
<?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, );
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
The photo may have a "orientation" data so that you can rotate it back to normal if you want. You may use the following codes right after the user has uploaded the image:
Please note that your server must have Imagick installed. (most new servers have)
<?php
function autoRotateImage($image) {
$orientation = $image->getImageOrientation();
switch($orientation) {
case imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateimage("#000", 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_RIGHTTOP:
$image->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
// Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image!
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
}
?>
<?php
$image = new Imagick('./sourcepath/'.$upload1);
autoRotateImage($image);
// - Do other stuff to the image here -
$image->writeImage('./destinationpath/'. $upload1);
?>

PHP imagecreatefromjpeg while keeping orientation

I have been working on my image upload website. I am trying to take pictures from my IPhone and upload them to my web server.
My files are uploading fine, However the problem i am running into is all of my images rotate 90 degrees to the left.
My Image upload process
$imageObject = imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name"]);
imagejpeg($imageObject, $target_file, 75);
Creating a new image and uploading it to my web directory. I create a new image to remove all of the EXIF Data (GPS location, all of my personal information)
The problem is that when i upload the image it does not save the file in portrait orientation (6). It doesn't actually save ANY orientation information. This being an obvious side effect of imagecreatefromjpeg. But all of my portrait style images save as landscape format.
My question is, is there any way for me to simply re-write the orientation Data into the NEW image after it is saved to my server?
Thank you all for your time!
You can read the exif information and use that to rotate or flip your image. Then you don't need the orientation data anymore.
Something like:
$imageObject = imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name"]);
# Get exif information
$exif = exif_read_data($_FILES["fileToUpload"]["tmp_name"]);
# Add some error handling
# Get orientation
$orientation = $exif['Orientation'];
# Manipulate image
switch ($orientation) {
case 2:
imageflip($imageObject, IMG_FLIP_HORIZONTAL);
break;
case 3:
$imageObject = imagerotate($imageObject, 180, 0);
break;
case 4:
imageflip($imageObject, IMG_FLIP_VERTICAL);
break;
case 5:
$imageObject = imagerotate($imageObject, -90, 0);
imageflip($imageObject, IMG_FLIP_HORIZONTAL);
break;
case 6:
$imageObject = imagerotate($imageObject, -90, 0);
break;
case 7:
$imageObject = imagerotate($imageObject, 90, 0);
imageflip($imageObject, IMG_FLIP_HORIZONTAL);
break;
case 8:
$imageObject = imagerotate($imageObject, 90, 0);
break;
}
# Write image
imagejpeg($imageObject, $target_file, 75);
Create Image maintains Aspect Ratio:
Answer is Here

Width and Height values are automatically swapped after uploading to server. [PHP]

I used this function getimagesize($file_tmp_name) to get width and height of the image I uploaded.
The function returned a 2D array: $width = arr[0] and $height = arr[1];
On my PC, the image demension is: 3024 x 4032 (w x h)
But on the server side, the image width and height values are swapped. 4032 x 3024 (w x h)
var_dump($width); // C:\wamp64\www\upload_script\Image.php:89:int 4032
var_dump($height); // C:\wamp64\www\upload_script\Image.php:90:int 3024
I'm not sure what caused this and how can I make it consistent. I appreciate any help.
You can check first comment on exif_read_data function in php manual.
Code copied from there:
<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>

Image magician and PNG uploads

I have a problem with GD when trying to upload png. The error which I've got is
Warning: imagepng(): gd-png error: compression level must be 0 through 9... on line 2474
on line 2474 in php_image_magician.php is case for png
case '.png':
// *** Scale quality from 0-100 to 0-9
$scaleQuality = round(($imageQuality/100) * 9);
// *** Invert qualit setting as 0 is best, not 9
$invertScaleQuality = 9 - $scaleQuality;
$this->checkInterlaceImage($this->isInterlace);
if (imagetypes() & IMG_PNG) {
imagepng($this->imageResized, $savePath, $invertScaleQuality);
} else { $error = 'png'; }
break;
and more specifically this
imagepng($this->imageResized, $savePath, $invertScaleQuality);
There is no errors while I upload jpg images..
Here is in my upload file where is the problem
$magicianObj = new imageLib($filepath);
$magicianObj->resizeImage(300, 300);
$magicianObj->saveImage($folderName . 'thumb/' . $filename, 300);
When I resize them,.
Rereading your post again it looks like you are having problems with all png files. What value are you using for the png image quality as this line recalculates it:
$scaleQuality = round(($imageQuality/100) * 9);
If you use an image quality in you code of 9 it will be changed to 0.81
Then again this line
$invertScaleQuality = 9 - $scaleQuality;
would convert the 0.81 to 8.19
Anyway I guess the problem is in the php file and not GD. I would echo some of the code out to see what values are being passed to the GD code.
You could also try hardcoding $invertScaleQuality to a value between 0 and 9 to see if that works.

Detect EXIF Orientation and Rotate Image using ImageMagick

Canon DSLRs appear to save photos in landscape orientation and uses exif::orientation to do the rotation.
Question: How can imagemagick be used to re-save the image into the intended orientation using the exif orientation data such that it no longer requires the exif data to display in the correct orientation?
Use the auto-orient option of ImageMagick's convert to do this.
convert your-image.jpg -auto-orient output.jpg
Or use mogrifyto do it in place
mogrify -auto-orient your-image.jpg
The PHP Imagick way would be to test the image orientation and rotate/flip the image accordingly:
function autorotate(Imagick $image)
{
switch ($image->getImageOrientation()) {
case Imagick::ORIENTATION_TOPLEFT:
break;
case Imagick::ORIENTATION_TOPRIGHT:
$image->flopImage();
break;
case Imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_BOTTOMLEFT:
$image->flopImage();
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_LEFTTOP:
$image->flopImage();
$image->rotateImage("#000", -90);
break;
case Imagick::ORIENTATION_RIGHTTOP:
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_RIGHTBOTTOM:
$image->flopImage();
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateImage("#000", -90);
break;
default: // Invalid orientation
break;
}
$image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
}
The function might be used like this:
$img = new Imagick('/path/to/file');
autorotate($img);
$img->stripImage(); // if you want to get rid of all EXIF data
$img->writeImage();

Categories