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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awesselCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.