/    Sign up×
Community /Pin to ProfileBookmark

Imagecreatefrompng

Hi everyone,
I’m in trick situation. When user upload images with png extension, it jump to this line of code.

[CODE]print ‘<br><font color=”#FF0000″>ERROR: Wrong filetype (has to be a .jpg or .jpeg. or gif.)</font>’; // Error Message If Filetype Is Wrong[/CODE]

But other images extension are uploading ok. help. Thanks in advance.

[CODE]case “image/png”:

$copy = copy($_FILES[‘imagefile’][‘tmp_name’], “$idir” . $_FILES[‘imagefile’][‘name’]); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It’s Permanent Location

// print ‘Image uploaded successfully.<br />’; // Was Able To Successfully Upload Image
$simg = imagecreatefrompng(“$idir” . $url); // Make A New Temporary Image To Create The Thumbanil From

$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height

if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width

} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors[‘red’], $colors[‘green’], $colors[‘blue’]); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagepng($dimg, “$tdir” . $url, 100); // Saving The Image

imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
// print ‘Image thumbnail created successfully.’; // Resize successful
}else {

print ‘<font color=”#FF0000″>ERROR: Unable to upload image.</font>’; // Error Message If Upload Failed
}

break;

case “image/gif”:
$copy = copy($_FILES[‘imagefile’][‘tmp_name’], “$idir” . $_FILES[‘imagefile’][‘name’]); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It’s Permanent Location

// print ‘Image uploaded successfully.<br />’; // Was Able To Successfully Upload Image
$simg = imagecreatefromgif(“$idir” . $url); // Make A New Temporary Image To Create The Thumbanil From

$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height

if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width

} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors[‘red’], $colors[‘green’], $colors[‘blue’]); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagegif($dimg, “$tdir” . $url, 100); // Saving The Image

imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
// print ‘Image thumbnail created successfully.’; // Resize successful
}else {

print ‘<font color=”#FF0000″>ERROR: Unable to upload image.</font>’; // Error Message If Upload Failed
}

break;

default:

print ‘<br><font color=”#FF0000″>ERROR: Wrong filetype (has to be a .jpg or .jpeg. or gif.)</font>’; // Error Message If Filetype Is Wrong

}[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJul 31.2010 — that's because your png files are not meeting any of the mime-types in your switch cases

[CODE]
case "image/png":
[/CODE]


Where and how are you getting the case values?
Copy linkTweet thisAlerts:
@diltonauthorAug 02.2010 — Thanks for your reply. there's

[CODE]switch($_FILES['imagefile']['type'])[/CODE]

Thank in advance
Copy linkTweet thisAlerts:
@diltonauthorAug 05.2010 — I'm still battling to find out the soluction about uploading image with png extension. tried used var_dump see the error. this message that it illustrated

[CODE]array(5) { ["name"]=> string(32) "QuandLeSapeurEnl--veLaWeston.png" ["type"]=> string(11) "image/x-png" ["tmp_name"]=> string(23) "C:wamptmpphp7879.tmp" ["error"]=> int(0) ["size"]=> int(116149) }[/CODE]

I can see the different between Image/png and image/x-png. But I can't solve the problem.Please help. Thank in advance
×

Success!

Help @dilton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...