CHanging uploaded image from GIF to JPG

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

CHanging uploaded image from GIF to JPG

Post by tristanlee85 »

In my gallery, only JPG thrumbnails will show up when using GD 2. I want to be able to resize GIF's so they also appear the same was as JPGs. Here is my config code:

Code: Select all

<?php

//Slide gallery variables
$col = 3; //no. of columns in a page
$maxrow = 3; //no. of rows in a page
$dir="."; //directory for this script, no need to change
$thumb = true ; //setting it to TRUE will generate real thumbnails on-the-fly, supports jpg file only and requires GD library. Setting it to FALSE will resize the original file to fit the thumbnail size, long download time. Turn it off if thumbnails don't show properly.
$place = "."; //directory of the slide mount images, no need to change
?>
Where you see "$thumb = true" is there any way I can make an 'if' statement so whenever there is a gif, it turns it to false, and back to true whenever there is a JPEG?
Last edited by tristanlee85 on Sat Dec 20, 2003 4:31 pm, edited 1 time in total.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You can load GIFs into GD and resize them etc but you can't export them as GIFs due to some legal reasons. The new version of GD will be released in 2004 and that will (we all hope) have full GIF support.
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

PNG is the way to go.. it's the improved (and free) version of GIF
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Ah.. but PNG doesn't support animated files as far as I know. I'm hoping that when the new version of GD comes along it will allow people to create animated GIFs.. that will make PNG pretty much redundant.

On top of that more browsers support GIF than PNG.. but that's another story :)
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

I'm only worried about JPG and GIF because people will only be uploading those types of files to my server. You said I can use GD to resize GIFs, but how?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You just need to load the GIF and then save it as a JPEG.
Something similar to this..

Code: Select all

<?php

// Load the GIF
$im = imagecreatefromgif("images/myImage.gif");

// Then save it as a JPEG
imagejpeg($im, "images/myImage.jpg", 80);

?>
It might be worth checking the manual and reading up on what GD functions are available to you... http://se.php.net/manual/en/ref.image.php
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Well, I obviously don't know how to do this as easy as I thought I could. Here is what I have and it isn't working. BTW, this thumbs.php. It used GD to create a thumbnail of the JPG. I tried to have it take the GIF and save it as a JPG, but I must have something wrong.

Code: Select all

<?php 
        $uploaddir = $_SERVER&#1111;'DOCUMENT_ROOT'] . "\\saturnspot&quote;;  //replace the $uploaddir to wherever you want the files to go 
        $filename = $_FILES&#1111;'file']&#1111;'name'];  // get the filename 
        $filetype = $_FILES&#1111;'file']&#1111;'type'];  // get the file MIME type 
        // Load the GIF 
		$im = imagecreatefromgif("$filename"); 

		// Then save it as a JPEG 
		imagejpeg($im, "$filename", 85); 

		$uploadfile = $uploaddir . $filename;  //this is full path to where the file will go 
        if (empty($filename)) &#123;  //make sure the user selected a file 
            die ("Please select a file to upload!"); 
        &#125; 
        if ($filetype == "image/jpeg" || $filetype == "image/jpg" || $filetype == "image/gif")  &#123;  //make sure file is .jpg or .gif 
          if (move_uploaded_file($_FILES&#1111;'file']&#1111;'tmp_name'], $uploadfile)) &#123; // move the file 
          echo "Thank you. The files was successfully uploaded.<br><br> To link to this files, please copy the following URL: <b>http://24.197.74.135/saturnspot/$filename</b>"; 
          &#125; 
          else &#123; 
          echo "File upload failed"; 
          &#125; 
        &#125; 
        else  &#123; 
        die ("Please only upload image files. Your file type was $filetype"); 
        &#125; 
?> 
 <html>
 <head></head>
 <body>
 <form action="index.php" method="post" ENCTYPE="multipart/form-data"> 
   <input type="file" size=40 name="file"><br> 
   <input type="hidden" name="MAX_FILE_SIZE" value="100000"> 
   <input type="submit" value="upload"> 
   </form>

To visit the SaturnSpot image gallery, click <a href="index.php">here</a>.
  </body>
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Maybe I should make this more clear: when the person uploads a GIF, i want it to automatically save it as a JPG instead of GIF.
Post Reply