Rotate and save an image - php

I'm already rotating an image:
$filename = 'http://gabomacias.zapto.org/flecha';
$grados = $_POST["grados"];
header('Content-type: image/png');
$source = imagecreatefrompng($filename);
$rotate = imagerotate($source, $grados, 0);
imagejpeg($rotate);
and showing it in the page,
But how can I save it so if someone rotates it again, it starts from the last rotation point and not form the original one, thanks.

From the imagejpeg documentation:
imagejpeg — Output image to browser or file
So,
imagejpeg($rotate, 'myimage.jpg');
ought to do the trick. At the top of the file, you can check to see if the myimage.jpg file exists using file_exists(), and if it does, load that to rotate it again.

Related

Convert any type of an image to PNG and add to PNG

I am trying to create an employee poster for a website that I develop. My goal is to take an image from a directory on the server of any file type (.png, .gif, .jpeg, etc.) and copy it onto another generate imaged that will then be outputted to the browser.
The problem is that I use:
$final_image = imagecreatefrompng("large_background.png");
for making the final image and for some reason if I add profile images with the type jpeg's, gif's, etc, (any type that isn't a jpeg) it doesn't work. The images never show up in the output. However, if I use png's it does work.
To solve this problem I tried converting the image to a png and then creating a png from it as shown in the code below. Unfortunately it doesn't work. Profile images still do not show up on the background.
// get image from database
$image_from_database = could be a .png, .jpeg, .gif, etc.
// get the image from the profile images directory
$path = "profile_images/".$image_from_database;
// create a png out of the image
$image = imagecreatefrompng(imagepng($path));
// add the $image to my larger $final_image (which is a png)
imagecopy($final_image, $image, $x, $y, 0,0, $height, $width);
imagepng($final_image, $ouput_url);
...
Can anybody tell me why this won't work? My profile images do not show up in the output of the final image.
My questions,
Is this line imagecreatefrompng(imagepng(...)); even possible? Essentially I want to convert an image of any type into a png and then create a png from it.
I'm just running a few local tests... The following works:
$src = imagecreatefromgif('test.gif');
$dest = imagecreatefrompng('test.png');
imagecopy($dest, $src, 0, 0, 0, 0, 100, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($src);
imagedestroy($dest);
So does this:
$src = imagecreatefromstring(file_get_contents('test.gif'));
If you're still having trouble after trying the latter example, please update your question. The actual images you're using would be helpful, in addition to a functional code example.
After you read an image with a imagecreatefrom* function, it doesn't matter what format the original was.
imagecreatefrom* functions return an image resource. When the image is loaded you are using internal representation of the images and not PNG, JPEG or GIF images.
If the images are successfully loaded imagecopy should have no problems with them.
This code uses images in different formats and works without a problem:
$img = imagecreatefrompng('bg.png');
$png_img = imagecreatefrompng('img.png');
$jpeg_img = imagecreatefromjpeg('img.jpeg');
$gif_img = imagecreatefromgif('img.gif');
/* or use this, so you don't need to figure out which imagecreatefrom* function to use
$img = imagecreatefromstring(file_get_contents('bg.png'));
$png_img = imagecreatefromstring(file_get_contents('img.png'));
$jpeg_img = imagecreatefromstring(file_get_contents('img.jpeg'));
$gif_img = imagecreatefromstring(file_get_contents('img.gif'));
*/
imagecopyresampled($img, $png_img, 10, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $jpeg_img, 120, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $gif_img, 230, 10, 0,0, 100, 100, 200, 200);
header('Content-Type: image/png');
imagepng($img);
Your example
$image = imagecreatefrompng(imagepng($path));
is wrong.
imagepng is used to output an image resource as a PNG image. If you provide a path as a second argument a PNG image file is created, otherwise it's printed to the output like echo does.
What imagepng actually returns is a boolean, indicating, if the output was successful.
You are then passing that boolean to imagecreatefrompng which expects a filepath. This is obviously wrong.
I suspect you have a problem with loading images.
imagecreatefrom* functions return FALSE on failure and you should check, if you have any problems with that.
Maybe your image paths are relative to doc root and your working directory is different.
Or you have permission problem.
Or your images are just missing.
It's impossible to tell from your question.

How to copy EXIF information from one file to another using PEL?

I have script that adds a watermark to some pictures in my photographic blog website. The watermarked file is served to the browser and leaves the original untouched. This part is working OK.
Recently I've found out that using simply GD for the watermark is stripping important information from the original file, the EXIF data. I've found that the solution is using PEL, so I need some help using it.
I was able to figure out in how to install PEL, but was unable to get it to copy EXIF data from $original_image to $new_image.
$jpeg = new PelJpeg($original_image);
$exif = $jpeg->getExif();
$tiff = $exif->getTiff();
$ifd0 = $tiff->getIfd();
$exif = $ifd0->getSubIfd(PelIfd::EXIF);
$ifd1 = $ifd0->getNextIfd();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
This also produces an error like this:
Catchable fatal error: Argument 1 passed to lsolesen\pel\PelJpeg::setExif() must be an instance of lsolesen\pel\PelExif, instance of lsolesen\pel\PelIfd given, called in /var/www/html/clerigo/exif.php on line 71 and defined in /var/www/html/clerigo/pel/src/PelJpeg.php on line 304.
[EDIT]
Ok, managed to make this work like this:
$jpeg = new PelJpeg($original);
$exif = $jpeg->getExif();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
ImageJPEG($new_image, "new_image.jpg");
$jpeg->saveFile("/var/www/html/clerigo/new_image.jpg");
The thing is, this is saving an image to a file, and the purpose is NOT to save any image, but serving it only to the browser on request, like this:
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
$jpeg->saveFile($new_image);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
imagedestroy($new_image);
But, this results in an error:
Warning: file_put_contents() expects parameter 1 to be a valid path, resource given in /var/www/html/clerigo/pel/src/PelJpeg.php on line 600
Any ideas how to solve this?
Take a close look at what you're doing here:
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
You're creating a new image called $jpeg and applying the EXIF data to it, but then you're outputting $new_image with ImageJPEG(). You'll need to make a call to PelJpeg::saveFile() to save the changes you've made and then serve up that file.
OK, figured it out, here is the final and working code:
$jpeg = new PelJpeg($original_image);
$exif = $jpeg->getExif();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
echo $jpeg->getBytes();

Create colorr image in php

Don't know what i'm doing wrong.
Want to create a simple solid color png image.
$im = #imagecreate(40, 40);
imagecolorallocate($im, '0xff', '0x00','0x00' );
$pdir = '/Applications/AMPPS/www/images/test.png';
imagepng($im,$pDir,9);
imagedestroy($im);
It doesn't work.
have no image in the dir even it it is ok for sure and has proper rights.
I only see some strange digits begins with PNG displayd on the page
To display your image, add the following to the very top of your script.
header("Content-type: image/png");
EDIT: Also, your variables for the output directory don't match. One is $pDir and $pdir.
EDIT 2: Here's your final code. imagepng() seems to either save the image to a file or display it, but not at the same time. So you will need two of them. Just simply don't pass a file path to the one that you want to display the image.
header("Content-type: image/png");
$im = #imagecreate(40, 40);
imagecolorallocate($im, '0xff', '0x00','0x00' );
$pdir = '/path/test.png';
imagepng($im);
imagepng($im,$pdir,9);
imagedestroy($im);

imagecopyresized() returning black thumbnails

I posted earlier about another resizing script not working and I got a little farther with this script which does things a little differently.
I got a little farther, only now there is a new problem. The first three lines of the code successfully place three identical files in the target directory with the file and it's two thumbnail files named accordingly. I then want to load the thumbnails, which are still full-size, and resize them but the script stops at imagecreatefromjpeg() and I can't seem to figure out why because $src has a value.
I thought that I could possibly remove that line and replace $source with $src in my imagecopyresized() function, and that gets me even closer. But it then returns a thumbnail of the target size, but the thumbnail is black.
move_uploaded_file($tmpFilePath, $newFilePath);
copy($newFilePath, $thumb500);
copy($newFilePath, $thumb200);
function thumbImage($src, $dest, $newheight) {
list($width, $height) = getimagesize($src);
$newwidth = $width * ($newheight / $height);
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($src);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb, $dest);
}
thumbImage($thumb500, $thumb500, 500);
thumbImage($thumb200, $thumb200, 200);
I feel as if this must be a common issue. Any suggestions anyone?
For me the supplied code block works if I use a JPEG image as source.
The problem may be that you use a PNG image that uses transparency. As JPEG can not handle transparencies, the transparent background colour will be filled. Maybe that is the problem. If not, please provide a sample image that has the problematic behaviour.
5
imagecopyresized takes an image resource as its second parameter, not a file name. You'll need to load the file first. If you know the file type, you can use imagecreatefromFILETYPE to load it. For example, if it's a JPEG, use imagecreatefromjpeg and pass that the file name - this will return an image resource.
If you don't know the file type, all is not lost. You can read the file in as a string and use imagecreatefromstring (which detects file types automatically) to load it as follows:
{$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
}
enter code here

Why is this creating a black image?

In the images folder i have a notfound.php file with
<?php
header('Content-type: image/png');
$im = imagecreatefrompng('simnotfound.png');
imagepng($im);
imagedestroy($im);
?>
The image is a 256 by 256. The notfound.php page shows a black 256 by 256 square. The image is not all black tho. Its just some black text on a transparent background in the center.
The fix is
<?php
header('Content-type: image/png');
$im = imagecreatefrompng('simnotfound.png');
imagealphablending($im, true); // setting alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
imagepng($im);
imagedestroy($im);
?>
create your image file one more time with white background to check that is read correctly, if yes - problem is your transparent background
also try with other file to eliminate problem with reading this specific file
bool imagesavealpha ( resource $image , bool $saveflag )
imagesavealpha — Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images

Categories