Save all uploaded images (png, gif ) as jpg using php - php

How i can save all uploaded images as jpg using php.
i have code php to upload images like this
$upload_dir="../uploads/";
$filename = $_FILES['pic1']['name'];
$tmp_name=$_FILES['pic1']['tmp_name'];
$path=$upload_dir.$filename;
move_uploaded_file($tmp_name, $path);
i am using
$image_path=imagecreatefromjpeg($path);
`imagejpeg($image_path);`
but not work!.

This won't work because you tell PHP to create image from jpeg format and your uploaded file is gif or png. You could use imagecreatefromstring() function:
$image_path = imagecreatefromstring(file_get_contents($path));
imagejpeg($image_path);

Related

To convert image by image magic in php

I have to convert a gif image to png image. For which i have tried this. The image file is in folder ( localhost ).
I have tried the below code.
$imageFile = "14981180607.gif";
$temp = sys_get_temp_dir();
file_put_contents($temp, file_get_contents($imageFile) );
exec("convert ".escapeshellarg($temp)." img/xx_%05d.png");
I need the image to be converted and store in my img folder, which i mentioned the path.

PHP imagecreatefrom gif() not working for animated gif

I'm letting users upload image files from their desktop and then using the PHP image routines to create thumbnails and resize images for the user's directory. But this doesn't seem to work at all for an animated gif. imagecreatefromgif(), for example, seems to peel off the first image of the animation and only work with it. Do I need to completely bypass these functions to get the complete uploaded animated gif to the user's directory?
Thanks
You cannot resize an animated gif image in the way you easily do with jpeg, bmp or other image formats. The function imagecreatefromgif() is not going to help you by any means, so take a look at the below link.
http://phpimageworkshop.com/tutorial/5/manage-animated-gif-with-imageworkshop.html
It should help you solve your problem.
To upload animated gif images I use this script:
$fileinfo = PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"../avatar/" . $newFilename);
$location="avatar/" . $newFilename;
It is simple and may not be a very functional one, but with animated gifs I have no problems.

Convert animated gif into PNG via PHP

The following is the code I'm using in order to convert the GIF file into a PNG file and save it into a variable:
$art = $_FILES["art"]["name"];
$art_ext = pathinfo($art, PATHINFO_EXTENSION);
if(strtoupper($art_ext)=="GIF"){
$art = imagepng(imagecreatefromstring(file_get_contents($_FILES["art"]["tmp_name"])), $art."png");
}
if($art!=""){
move_uploaded_file($art, "images/".$art );
}
Also, is there a method for converting an animated gif to a static gif?
This code should work. I assume you are following the example at Convert JPG/GIF image to PNG in PHP? To convert a gif to a static jif you could honestly just rename the png file you created with your current code to jif. All modern web-browsers and applications will treat a static jif and a png the same.
To get the first frame of an animated gif, you can use imagecreatefromgif and to save as a gif, you use imagegif, so it would be:
$art = imagegif(imagecreatefromgif($_FILES["art"]["tmp_name"]), $art."gif");

Converting PNG to JPEG file

I have a script to convert PNG files to JPEG files. Except, I'm not exactly sure how it works. What do use for $outputPngFile and $outputJpgFile? Can I do this with a tmp file, like when the user is uploading it? Then, how do I access the new file to move it to the proper image directory?
function pngTojpg($image, $outputPngFile, $outputJpgFile, $quality) {
$image = imagecreatefrompng($image);
//Save the png image
imagepng($image, $outputPngFile);
//Save the jpeg image
imagejpeg($image, $outputJpgFile, $quality);
// Free up memory
imagedestroy($image);
}
<?php
$image = imagecreatefrompng('yourlocation/image.png');
imagejpeg($image, 'yournewlocation/image.jpg', 70);
imagedestroy($image);
?>
It would probably help you to know that you're using the GD library that has been bundled with PHP.
What the function is doing is taking a path to a png image ($image), loading it into a GD resource that can be manipulated within PHP (imagecreatefrompng), saving the image as a png to the png output path ($outputPngFile), then saving the image as a jpg to the jpg output path ($outputJpgFile) with a particular compression factor ($quality), and finally destroying the image resource object, since it isn't needed anymore.
Since it's saving the image as a png as well, the function was obviously intended to be used to save an image from either an external source (given by a URL) or temporary files from a user upload. You can do either, PHP doesn't care so long as the path you provide to the image file is valid.

Convert jpg image to gif, png & bmp format using PHP

How can I convert a single jpg image into 3 different image formats, gif, png and bmp, using PHP?
You first create an image object out of your file with imagecreatefromjpeg(). You then dump that object into different formats (using imagegif() for example):
$imageObject = imagecreatefromjpeg($imageFile);
imagegif($imageObject, $imageFile . '.gif');
imagepng($imageObject, $imageFile . '.png');
imagewbmp($imageObject, $imageFile . '.bmp');
Use libGD — http://www.php.net/manual/en/book.image.php
I've set up a new opensource project on Github that allows reading and saving of BMP files (actual BMP files, not wbmp), and other file formats, in PHP. It's nice and easy to use.
The project is called PHP Image Magician.

Categories