使用 PHP 脚本将上传的图像转换为 PNG
我在 Flash 中制作了一个上传按钮,链接到 PHP 脚本,我想知道是否有任何方法可以在用户上传图像时将所有图像转换为 PNG?这是我到目前为止所拥有的,但它似乎对我不起作用,有人能指出我正确的方向吗?
<?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";
}
?>
I've got an upload button I've made in Flash that 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";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看我的一些代码,我认为
应该阅读
或类似的内容。您可能想使用
basename
或类似的内容删除扩展。 (http://php.net/manual/en/function.basename.php)Looking at some of my code, I think that
should read
Or something similar. You probably want to remove the extension using
basename
or something similar. (http://php.net/manual/en/function.basename.php)