PHP Rotate image file permanently - php

I want a user to be able to permanently rotate an image file clockwise or counter clockwise. I have tried imagejpeg($rotate) but can't seem to make it work right.
<form method="GET" action="rotate.php">
Rotate:<input type="radio" name="rotate" value="clockwise">Clockwise
<input type="radio" name="rotate" value="counterclockwise">Counter clockwise
<input type="Submit" name="Submit1"/>
</form>
I am trying to allow the user to be able to choose the radio button direction and click "Submit". Then the displayed image will update rotated to whichever direction they chose and stay that way permanently whenever it is used again. Any help or direction?
<img src=\"uploads/$user/$folder/$image\"/></a>";

use imagerotate() this will rotate image permanently.
<?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);
?>
http://php.net/manual/en/function.imagerotate.php
Or you can use this using jquery
http://www.linein.org/examples/jquery_rotate/

If you want to rotate your picture permanently, HTML or CSS is not the way to go. You will need some server-side scripting to store the rotated picture.
You sould have a look at GD library. For this kind of image manipulation, I have been using Imagine library, which is a library based on GD.

If someone else ever needs this. Here is a function that will replace the original with the rotated image:
public function autoRatateImage($src, $exifCode = ''){
if($exifCode == ''){
$exif = exif_read_data($src);
}else{
$exif['Orientation'] = $exifCode;
}
$source = imagecreatefromjpeg($src);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($source, 180, 0);
break;
case 6:
$image = imagerotate($source, -90, 0);
break;
case 8:
$image = imagerotate($source, 90, 0);
break;
}
if (file_exists($src)) {
unlink($src);
}
imagejpeg($image, $src , 100);
}
}

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 Rotate Image from request file

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).

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 imagejpeg()

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.

change two images in php

EDIT:
I have to images (or two strings) on a page. One on the left and the other on the right side. I want them to randomly switch their position when refreshing the site. How can I do this? Thanks.
Try this
$image1 = echo "<img src='image_path'>";
$image2 = echo "<img src='image_path'>";
if(rand(0,1)==0){
$image3 = $image1;
$image1 = $image2;
$image2 = $image3;
}
<div style="float:right">$image1</div>
<div style="float:left">$image2</div>
Use imagerotate() and rand() functions.
You can use imagerotate for this thing. The documentation has this simple example:
<?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);
So, just write a script that takes a filename as a parameter (via $_GET) and then rotate it by a random degree (use rand for this). Then just change your image link to rotator.php?file=first.jpg.
Another option is to do the rotation with CSS3 and JavaScript. See this link for more information on rotating with CSS3.

Categories