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 Like 8 Dislike
Previous forums Next forums
Other forums

Change Web page language
i doing this thing first time but i dont find any suitable solution for it. On the demand of user. I

This is driving me nuts!
This insert query looks to be alright, however I get this error:

QuoteYou have an error in yo

At max how many columns is advisable to create in a table/view
Hi All,
I have two transaction table from which i want to create a simple view or material

How to have a log of all the status a VIM document had been ?
Hi guys,

I’m using SAP Invoice Management plugin and the /OPT/VIM_ANALYTICS transa

Calling variables
I want to call this to my website but it doesn't display what I want it to,

Code: <h5&

a dificult string search
Hi I don't know a way around this. I want the user to input a password, but to make it a bit complca

Basic Forum Tutorial
Hi, I'm new to PHP. I want to build a basic forum for my site using PHP and MySQL. I've searched t

A href problem under php
Hello...

I tried to explain the issue in an earlier post.. but was not clear enough....

Tracking Unique Clicks
Hi guys and gals,

Basically I have a database which tracks clicks on my outbound links, I do

Help on Order Entry Form/System Where is best to begin.
I have a dilema and a very short amount of time at this point and I'm looking for some help on decid

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash