Converting uploaded images to PNG with PHP script
I've got an upload button I've made in Flash th开发者_开发知识库at links to a PHP script and I was wondering if there was any way I could convert all the images to PNGs as the user uploads them? This is what I have so far, but it doesn't seem to be working for me, can anyone point me in the right direction?
<?php
if (move_uploaded_file($_FILES['Filedata']['tmp_name'],
"Parts/userdata/" . $_FILES['Filedata']['name']))
{
switch ($_FILES['Filedata']['type']) {
case ".jpg": case ".jpeg":
$image = imagecreatefromjpeg($_FILES['Filedata']['name']);
imagepng($image, $_FILES['Filedata'] . 'png');
imagedestroy($image);
break;
case "gif":
$image = imagecreatefromgif($_FILES['Filedata']['name']);
imagepng($image, $_FILES['Filedata'] . 'png');
imagedestroy($image);
break;
default:
echo "PNG Given";
}
echo "OK";
}
else
{
echo "ERROR";
}
?>
Looking at some of my code, I think that
imagepng($image, $_FILES['Filedata'] . 'png');
should read
imagepng($image, $_FILES['Filedata']['name'] . 'png');
Or something similar. You probably want to remove the extension using basename
or something similar. (http://php.net/manual/en/function.basename.php)
精彩评论