I am trying to rotate an image through GD Library and a database value:
// Image for rotating
$image = 'images/weather-icons/windDir.png';
//degrees from database
$degrees = $row[WindDirDegrees];
// Create the canvas
$source = imagecreatefrompng($image) ;
// Rotate the image
$rotate = imagerotate($source, $degrees, 0) ;
// Outputs
imagepng($rotate) ;
But all I get as output is a huge amount of "gibbergabber" symbols.
Does anyone know why?
You are missing header('Content-Type: image/png');
See: http://www.php.net/manual/en/function.imagepng.php
Related
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);
?>
I am now using the following method to store an image to server from input file type
$image = $request->file('file');
$filename = $item->itemId . '.png';
Storage::disk('s3')->put('/'.$filename, file_get_contents($image), 'public');
and I found method to rotate the image with PHP
$filename = 'test.jpg';
$degrees = 180;
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($filename);
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate);
but I dont know how implement the code with $request->file('file')
Thanks!
I am not sure where you are getting your functions from, imagecreatefromjpeg() and imagerotate(), but if you just use PHP's own, available functions (provided by Imagick), you can do something much simpler...
$image = new Imagick();
$image_filehandle = fopen('some/file.jpg', 'a+');
$image->readImageFile($image_filehandle );
$image->rotateImage("FFFFFF", 90); # Rotate 90 degrees, keep background of "FFFFFF" (white)
$image_icon_filehandle = fopen('some/file-rotated.jpg', 'a+');
$image->writeImageFile($image_icon_filehandle);
The background color ("FFFFFF") is applied here if the image rotates and leaves a certain amount of background (does not happen for rotate degrees in increments of 90).
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
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.
I am trying to put a rotate img function on my site. I am using imagejpeg() but it returns a sloo full of gibberish. Can you explain why?
if ($_GET["rotate"] == "clockwise")
{
$degrees = 90;
// Content type
//header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($path);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
if (imagejpeg($rotate))
echo "Your image has been rotated clockwise";
}
if ($_GET["rotate"] == "counterclockwise")
{
$degrees = 270;
// Content type
//header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($path);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
if (imagejpeg($rotate))
echo "Your image has been rotated Counterclockwise";
}
?>
It pastes this onto the page:
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀûÛ"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á (AND ON AND ON FOR A LONG WAY)
If I change it to clockwise the gibberish changes as well, so I think it's working somewhat, but it's not creating a jpg from it. Any help would be awesome.
That is the image. You're just not telling your browser that it is, your browser interprets the data as text. Set a header to tell your browser to interpret the data as image:
header('Content-Type: image/jpeg');
The image data will have to be the only thing output on the page, no other HTML or text before or after it.