How to convert gif to jpg

Posted on 2006-11-04
Last Modified: 2008-02-01

I upload gifs which I want to convert and save as jpgs. I do this:

$image = @imagecreatefromgif($filename);
  $image_p = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  imagejpeg($image_p, $filename_tn, 100); // 100% quality

Will that do the job, or is something else needed?

Question by:tr5
  • 2
  • 2
LVL 35

Accepted Solution

Raynard7 earned 500 total points
ID: 17875659
Hi,  that should be sufficient

the colour looks right, the conversion looks right and saving the jpeg looks right.

The only thing I would consider is if you are uploading these to be converted - why not convert the $_FILE to a jpeg on the fly rather than saving the gif to file and then re sampling it?

Author Comment

ID: 17875689
How would I do that?
LVL 35

Expert Comment

ID: 17875694
well - you have $image

= imagecreatefromgif($filename)

you should just be able to point it to the uploaded file

= imagecreatefromgif($_FILE['uploadedFileName']);

and the rest of the script would remain the same

Author Comment

ID: 17875710
ok. Thanks.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question