Png Jpg Gif Upload

This is the code (below) i have, which uploads Jpegs, and Pngs only, but it dont upload Gifs, neither does it upload with the transparency.

What i was trying to do is upload the images formats, some of with transparency, and save to a folder as a logo.gif





=================================================
<HTML>
  <HEAD>
    <TITLE>Image Uploaded</TITLE>
<?
$opt=$_REQUEST['opt'];  

?>

  </HEAD>

  <BODY>
  <?php

 

    // Define the variables
    $myPath = "./../image";
    $myImage = $_FILES['fileupload']['name'];
    $imgSrc = $_FILES['fileupload']['tmp_name'];
    $fileType = $_FILES['fileupload']['type'];
    $imgDst = "$myPath/logo.gif";//  $myImage
    $imgMaxH = 60;
    $imgMaxW = 200;
if($imgSrc == '' ){
        echo "<center><B><font color='red'>Please choose a file first.</font></B></center></P>";
$opt = 0;
}

if ($opt==1) {
    If ($fileType == "image/png") {
      $wtype = imagecreatefrompng($imgSrc);
        }
      if ($fileType == "image/jpeg" or $fileType == "image/jpg") {
      $wtype = imagecreatefromjpeg($imgSrc);
        }      
      
   
      $imgSrcID = $wtype;
      // if we were succesful we get the uploaded image size...
      If ($imgSrcID) {
        $imgSrcInfo = getimagesize($imgSrc);
        $imgSrcW = $imgSrcInfo[0]; //img width
        $imgSrcH = $imgSrcInfo[1]; //img height

        // ... and we calculate a scale factor to resize the image with
        If ($imgSrcH < $imgSrcW) {
          $imgScale = $imgMaxH / $imgSrcH;
        }
            //Else {
//          $imgScale = $imgMaxW / $imgSrcW;
//        }

        // ... and use the scale factor to calculate the resized dimensions
        $imgH = (int) ($imgSrcH * $imgScale);
        $imgW = (int) ($imgSrcW * $imgScale);


        // now we can create the destination image...
        $imgDstID = imagecreatetruecolor($imgW, $imgH);

        // ... and copy the source image to the destination image and resize it.
        If (imagecopyresampled($imgDstID, $imgSrcID, 0, 0, 0, 0, $imgW, $imgH, $imgSrcW, $imgSrcH)) {
          // and save it to the destination (85 = the image quality)
          imagejpeg($imgDstID, $imgDst, 100);
        }

        // destroy the image links...
        imagedestroy($imgDstID);
        imagedestroy($imgSrcID);

        echo "<center><B><font color='green'>".$myImage." has been successfully uploaded!</font></B></center></P>";
      ?>
      <center>
    <h1>Upload Your Logo</h1>
      <FORM NAME="myForm" ENCTYPE="multipart/form-data" ACTION="logo.php?opt=1" METHOD="POST">
        <INPUT TYPE="file" NAME="fileupload"><br><br>
        <INPUT NAME="Submit" TYPE="submit" VALUE="Upload">
    </FORM><p><h2>Your Current Uploaded Logo</h2><br><img src='./../image/logo.gif' bolder='0'/><p>
        <INPUT NAME="close" onClick="javascript:window.close()" TYPE="button" VALUE="Close Window"></center>
<?

    }
      }
      else
      {
      ?>
      <center>
    <h1>Upload Your Logo</h1>

      <FORM NAME="myForm" ENCTYPE="multipart/form-data" ACTION="logo.php?opt=1" METHOD="POST">
        <INPUT TYPE="file" NAME="fileupload"><br><br>
        <INPUT NAME="Submit" TYPE="submit" VALUE="Upload">
    </FORM><p><h2>Your Current Uploaded Logo</h2><br><img src="./../image/logo.gif" bolder='0'/><p>
   
        <INPUT NAME="close" onClick="javascript:window.close()" TYPE="button" VALUE="Close Window"></center>
<?
}



?>
 

  </BODY>
</HTML>
LVL 8
Sam CohenConsultant to Digital/DeveloperAsked:
Who is Participating?
 
awesselConnect With a Mentor Commented:
There are a couple problems.  The first is that you're checking only for png and jpeg/jpg formats.  The second is that you're outputting the image as a jpg, not a gif.

A third possible issue is unclear according to the PHP docs.  The imagecreatetruecolor() function does not work with the GIF format according to the documentation, but in the example on how to export a gif, it uses that exact function.  If the code below doesn't give you the desired results, I'd change the imagecreatetruecolor() reference to imagecreate().

Adam
<HTML>
  <HEAD>
    <TITLE>Image Uploaded</TITLE>
<?
$opt=$_REQUEST['opt'];
?>
  </HEAD>
 
  <BODY>
  <?php
    // Define the variables
    $myPath = "./../image";
    $myImage = $_FILES['fileupload']['name'];
    $imgSrc = $_FILES['fileupload']['tmp_name'];
    $fileType = $_FILES['fileupload']['type'];
    $imgDst = "$myPath/logo.gif";//  $myImage
    $imgMaxH = 60;
    $imgMaxW = 200;
    
    if($imgSrc == '' ) {
      echo "<center><B><font color='red'>Please choose a file first.</font></B></center></P>";
      $opt = 0;
    }
    
    if ($opt==1) {
      switch($fileType){
        case 'image/png':
          $imgSrcID = imagecreatefrompng($imgSrc);
          break;
        case 'image/jpg':
        case 'image/jpeg':
          $imgSrcID = imagecreatefromjpeg($imgSrc);
          break;
        case 'image/gif':
          $imgSrcID = imagecreatefromgif($imgSrc);
          break;
      }
      
      // if we were succesful we get the uploaded image size...
      if ($imgSrcID) {
        $imgSrcInfo = getimagesize($imgSrc);
        $imgSrcW = $imgSrcInfo[0]; //img width
        $imgSrcH = $imgSrcInfo[1]; //img height
        
        // ... and we calculate a scale factor to resize the image with
        if ($imgSrcH < $imgSrcW) {
          $imgScale = $imgMaxH / $imgSrcH;
        }
        //else {
        //  $imgScale = $imgMaxW / $imgSrcW;
        //}
 
        // ... and use the scale factor to calculate the resized dimensions
        $imgH = (int)($imgSrcH * $imgScale);
        $imgW = (int)($imgSrcW * $imgScale);
 
        // now we can create the destination image...
        $imgDstID = imagecreatetruecolor($imgW, $imgH);
 
        // ... and copy the source image to the destination image and resize it.
        if (imagecopyresampled($imgDstID, $imgSrcID, 0, 0, 0, 0, $imgW, $imgH, $imgSrcW, $imgSrcH)) {
          // and save it to the destination (85 = the image quality)
          imagegif($imgDstID, $imgDst, 100);
        }
 
        // destroy the image links...
        imagedestroy($imgDstID);
        imagedestroy($imgSrcID);
 
        echo "<center><B><font color='green'>".$myImage." has been successfully uploaded!</font></B></center></P>";
      ?>
      <center>
    <h1>Upload Your Logo</h1>
      <FORM NAME="myForm" ENCTYPE="multipart/form-data" ACTION="logo.php?opt=1" METHOD="POST">
        <INPUT TYPE="file" NAME="fileupload"><br><br>
        <INPUT NAME="Submit" TYPE="submit" VALUE="Upload">
    </FORM><p><h2>Your Current Uploaded Logo</h2><br><img src='./../image/logo.gif' bolder='0'/><p>
        <INPUT NAME="close" onClick="javascript:window.close()" TYPE="button" VALUE="Close Window"></center>
<?
      }
    } else {
?>
    <center>
      <h1>Upload Your Logo</h1>
      
      <FORM NAME="myForm" ENCTYPE="multipart/form-data" ACTION="logo.php?opt=1" METHOD="POST">
        <INPUT TYPE="file" NAME="fileupload"><br><br>
        <INPUT NAME="Submit" TYPE="submit" VALUE="Upload">
      
        <p><h2>Your Current Uploaded Logo</h2><br><img src="./../image/logo.gif" bolder='0'/><p>
        
        <INPUT NAME="close" onClick="javascript:window.close()" TYPE="button" VALUE="Close Window"></center>
      </FORM>
<? 
    }
?>
 
 
  </BODY>
</HTML>

Open in new window

0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
awessel.
Your code works a little. But it show the transparent area as black once uploaded
0
 
awesselCommented:
Have you tried using imagecreate()  instead of imagecreatetruecolor()?  The PHP docs are unclear as to which method works for creating gifs.

Another option is to try calling imagetruecolortopalette($imgDstID, true, 256); after creating the image with imagecreatetruecolor().

Are you tied to using gifs?  It may be easier to create transparent pngs instead.  See below:
$png = imagecreatetruecolor($imgW, $imgH);
imagesavealpha($png, true);
 
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);
 
//add your resampling here

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Where would i put that in the code you provided?
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Well , for some reason it working the same.

Well what i was trying to accomplish is to have a page to upload a gif, png, jpeg or jpg image
the max size is 200 pixels w x 60 pixels h. The height should dictate the proportion of any image larger than 200 x 60..The image should be resized down to the max 60pixels h.
Transparent supported, and all images should save as a PNG file (logo.png)

Yes , i would rather use a png than a gif
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Code is not working properly. What should i do?
0
 
awesselCommented:
Sorry for the delay.  I'll take a look at the code again
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
Never got the transparency to work. :(  but its ok. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.