cgray1223
asked on
convert all images to jpeg in php
Hello,
I have an image upload script that uses html and php and I'm currently relying on all images to be of type JPEG and if I use any other type then the image that gets created is all black. I'm using the imagejpeg function to create the image based off the uploaded file. Are there functions like imagejpeg that I can call for .gif and .png? Maybe I should convert all images into a jpeg? Any suggestions?
PHP code to write new file.
I have an image upload script that uses html and php and I'm currently relying on all images to be of type JPEG and if I use any other type then the image that gets created is all black. I'm using the imagejpeg function to create the image based off the uploaded file. Are there functions like imagejpeg that I can call for .gif and .png? Maybe I should convert all images into a jpeg? Any suggestions?
PHP code to write new file.
$imageFile = $_FILES['image']['tmp_name'];
$filename = basename( $_FILES['image']['name']);
list($width, $height) = getimagesize($imageFile);
$src = imagecreatefromjpeg($imageFile);
$orig_h = ($height/$width)* $orig_w;
$tmp = imagecreatetruecolor($orig_w, $orig_h);
imagecopyresampled($tmp, $src, 0,0,0,0,$orig_w,$orig_h,$width,$height);
imagejpeg($tmp, $folder.$filename,100);
imagedestroy($tmp);
imagedestroy($src);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Apart from these basic functions you can actually use something much more powerful like ImageMagick.
You can have a look here: http://www.imagemagick.org/script/index.php
Anyway, this is how much you can actually do with it.
* Format conversion: convert an image from one format to another (e.g. PNG to JPEG).
* Transform: resize, rotate, crop, flip or trim an image.
* Transparency: render portions of an image invisible.
* Draw: add shapes or text to an image.
* Decorate: add a border or frame to an image.
* Special effects: blur, sharpen, threshold, or tint an image.
* Animation: create a GIF animation sequence from a group of images.
* Text & comments: insert descriptive or artistic text in an image.
* Image identification: describe the format and attributes of an image.
* Composite: overlap one image over another.
* Montage: juxtapose image thumbnails on an image canvas.
* Generalized pixel distortion: correct for, or induce image distortions including perspective.
* Morphology of shapes: extract features, describe shapes and recognize patterns in images.
* Motion picture support: read and write the common image formats used in digital film work.
* Image calculator: apply a mathematical expression to an image or image channels.
* Discrete Fourier transform: implements the forward and inverse DFT.
* High dynamic-range images: accurately represent the wide range of intensity levels found in real scenes ranging from the brightest direct sunlight to the deepest darkest shadows.
* Encipher or decipher an image: convert ordinary images into unintelligible gibberish and back again.
* Virtual pixel support: convenient access to pixels outside the image region.
* Large image support: read, process, or write mega-, giga-, or tera-pixel image sizes.
* Threads of execution support: ImageMagick is thread safe and most internal algorithms are OpenMP-enabled to take advantage of speed-ups offered by multicore processor chips.
* Heterogeneous distributed processing: certain algorithms are OpenCL-enabled to take advantage of speed-ups offered by executing in concert across heterogeneous platforms consisting of CPUs, GPUs, and other processors.
* ImageMagick on the iPhone: convert, edit, or compose images on your iPhone.
If imagemagic is installed on your server (most unix hosts have it) its as simple as this (assuming you have a tiff file called file.tiff that you want converted into file.jpg) :
<?php
$exec = "convert /path/to/file.tiff /path/to/file.jpg";
exec($exec, $yaks);
//to view any errors >> // print_r($yaks);
?>
Cheers,
Ultra_Master
You can have a look here: http://www.imagemagick.org/script/index.php
Anyway, this is how much you can actually do with it.
* Format conversion: convert an image from one format to another (e.g. PNG to JPEG).
* Transform: resize, rotate, crop, flip or trim an image.
* Transparency: render portions of an image invisible.
* Draw: add shapes or text to an image.
* Decorate: add a border or frame to an image.
* Special effects: blur, sharpen, threshold, or tint an image.
* Animation: create a GIF animation sequence from a group of images.
* Text & comments: insert descriptive or artistic text in an image.
* Image identification: describe the format and attributes of an image.
* Composite: overlap one image over another.
* Montage: juxtapose image thumbnails on an image canvas.
* Generalized pixel distortion: correct for, or induce image distortions including perspective.
* Morphology of shapes: extract features, describe shapes and recognize patterns in images.
* Motion picture support: read and write the common image formats used in digital film work.
* Image calculator: apply a mathematical expression to an image or image channels.
* Discrete Fourier transform: implements the forward and inverse DFT.
* High dynamic-range images: accurately represent the wide range of intensity levels found in real scenes ranging from the brightest direct sunlight to the deepest darkest shadows.
* Encipher or decipher an image: convert ordinary images into unintelligible gibberish and back again.
* Virtual pixel support: convenient access to pixels outside the image region.
* Large image support: read, process, or write mega-, giga-, or tera-pixel image sizes.
* Threads of execution support: ImageMagick is thread safe and most internal algorithms are OpenMP-enabled to take advantage of speed-ups offered by multicore processor chips.
* Heterogeneous distributed processing: certain algorithms are OpenCL-enabled to take advantage of speed-ups offered by executing in concert across heterogeneous platforms consisting of CPUs, GPUs, and other processors.
* ImageMagick on the iPhone: convert, edit, or compose images on your iPhone.
If imagemagic is installed on your server (most unix hosts have it) its as simple as this (assuming you have a tiff file called file.tiff that you want converted into file.jpg) :
<?php
$exec = "convert /path/to/file.tiff /path/to/file.jpg";
exec($exec, $yaks);
//to view any errors >> // print_r($yaks);
?>
Cheers,
Ultra_Master
imagecreatefromgif()