Sam Cohen
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']['nam e'];
$imgSrc = $_FILES['fileupload']['tmp _name'];
$fileType = $_FILES['fileupload']['typ e'];
$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($imgSr c);
}
$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($imgDs tID, $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></cent er></P>";
?>
<center>
<h1>Upload Your Logo</h1>
<FORM NAME="myForm" ENCTYPE="multipart/form-da ta" 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-da ta" 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>
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']['nam
$imgSrc = $_FILES['fileupload']['tmp
$fileType = $_FILES['fileupload']['typ
$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>
$opt = 0;
}
if ($opt==1) {
If ($fileType == "image/png") {
$wtype = imagecreatefrompng($imgSrc
}
if ($fileType == "image/jpeg" or $fileType == "image/jpg") {
$wtype = imagecreatefromjpeg($imgSr
}
$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
// ... and copy the source image to the destination image and resize it.
If (imagecopyresampled($imgDs
// 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."
?>
<center>
<h1>Upload Your Logo</h1>
<FORM NAME="myForm" ENCTYPE="multipart/form-da
<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
<?
}
}
else
{
?>
<center>
<h1>Upload Your Logo</h1>
<FORM NAME="myForm" ENCTYPE="multipart/form-da
<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
<?
}
?>
</BODY>
</HTML>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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($i mgDstID, 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:
Another option is to try calling imagetruecolortopalette($i
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
ASKER
Where would i put that in the code you provided?
ASKER
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
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
ASKER
Code is not working properly. What should i do?
Sorry for the delay. I'll take a look at the code again
ASKER
Never got the transparency to work. :( but its ok. :)
ASKER
Your code works a little. But it show the transparent area as black once uploaded