?
Solved

Png Jpg Gif Upload

Posted on 2008-11-10
8
Medium Priority
?
2,018 Views
Last Modified: 2012-05-05
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>
0
Comment
Question by:Sam Cohen
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
awessel earned 600 total points
ID: 22927815
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
 
LVL 8

Author Comment

by:Sam Cohen
ID: 22932705
awessel.
Your code works a little. But it show the transparent area as black once uploaded
0
 
LVL 4

Expert Comment

by:awessel
ID: 22935430
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Author Comment

by:Sam Cohen
ID: 22936793
Where would i put that in the code you provided?
0
 
LVL 8

Author Comment

by:Sam Cohen
ID: 22936868
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
 
LVL 8

Author Comment

by:Sam Cohen
ID: 22973402
Code is not working properly. What should i do?
0
 
LVL 4

Expert Comment

by:awessel
ID: 22991736
Sorry for the delay.  I'll take a look at the code again
0
 
LVL 8

Author Closing Comment

by:Sam Cohen
ID: 31515390
Never got the transparency to work. :(  but its ok. :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 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