Link to home
Start Free TrialLog in
Avatar of Sam Cohen
Sam CohenFlag for United States of America

asked on

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>
ASKER CERTIFIED SOLUTION
Avatar of awessel
awessel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sam Cohen

ASKER

awessel.
Your code works a little. But it show the transparent area as black once uploaded
Avatar of awessel
awessel

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

Where would i put that in the code you provided?
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
Code is not working properly. What should i do?
Sorry for the delay.  I'll take a look at the code again
Never got the transparency to work. :(  but its ok. :)