image upload script not working with png
Posted on
16th Feb 2014 07:03 pm by
admin
The basic process of this php code is to take the submitted file, convert it to a small JPG thumbnail and put it into one directory and save the original file into another, but it seems that when using PNG images it doesn't quite work, any ideas?
Code: if((($_FILES['file']['type'] == "image/gif")
|| ($_FILES['file']['type'] == "image/jpeg")
|| ($FILES['file']['type'] == "image/pjpeg"))
&& ($_FILES['file']['size'] < 2000000000))
{
if($_FILES['file']['error'] > 0)
{
echo '<script language="javascript" type="text/javascript">window.top.window.upload_error(' . $_FILES['file']['error'] . ');</script>';
}
else
{
$fileName = $_FILES['file']['name'];
$f_ext = "." . strtolower(substr(strrchr($fileName, '.'), 1));
$time = time();
$f_noext = $time.substr(microtime(),2,3);
$f_name = $time.substr(microtime(),2,3) . $f_ext;
if(file_exists("img/" . $f_name))
{
echo $_FILES['file']['name'] . " already exists.";
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'], "img/" . $f_name);
$image_path = "img/" . $f_name;
$fsize = getimagesize($image_path);
$max_d = ($orig_p==0)? 250 : 125;
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if($ext == 'jpg' || $exit == 'jpeg')
{
$img = @imagecreatefromjpeg($image_path);
}
else if($ext == 'png')
{
$img = @imagecreatefrompng($image_path);
}
else if ($ext == 'gif')
{
$img = @imagecreatefrompng($image_path);
}
if($img)
{
$w = imagesx($img);
$h = imagesy($img);
$width = imagesx($img);
$height = imagesy($img);
$scale = min($max_d/$width, $max_d/$height);
if($scale < 1){
$new_width = floor($scale *$width);
$new_height = floor($scale*$height);
$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0,0,0,0,$new_width,$new_height,$width,$height);
imagedestroy($img);
$img = $tmp_img;
ImageJPEG($img, 'thumb/' . $f_noext . 't.jpg',60);
imagedestroy($tmp_img);
echo '<script language="javascript" type="text/javascript">window.top.window.stopUpload();</script>';
}
}
}
}
}
No comments posted yet
Your Answer:
Login to answer
50
8
Other forums
IIS7 and getimagesize() problem
I have just discovered after hrs of trying to fix a problem where it's coming from. It's to do with
Add "width" check when upload
Hi!
Can someone add a function that is checking if picture witdh is bigger then 800px whwn up
need Array help
This is what I have to do.
$teamname[1] = "Red Sox"
$teamname[2] = "Gian
Taylor Series Function
So I am working a a project for my intro C++ class, and I am having some trouble with a function for
[RESOLVED] Socket/Port remains open after app crashes
I'm having this problem with a networked app in vb.net.
If the program exits normally the por
Help, Base64 encoding url and decoding server side.
Hello everyone, i have a website with videos and games and also provide an embed code which people c
Big Problem!! Please help
Hi Guys,
Im making a website for a friend have encountered a really annoying problem. When ev
Help with search.php
Hello All
I need the search engine on a site I am building at the moment to display the searc
Replacing a string on click of a button
Hi! Is there a way to replace a string on a click of a text link?
This is what I've come up w
iterating through an array and escape each value independently.
I have a set up where the variable being escaped is an array and it needs to be iterated and escaped