在将服务器png / gif保存到jpg之前转换DURING上传 [英] converting DURING upload before saving on server png/gif to jpg

查看:105
本文介绍了在将服务器png / gif保存到jpg之前转换DURING上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个图片上传脚本。
它上传图像并将其保存到服务器上的空间。
我似乎无法理解的是,当用户上传.png时,当它保存在我的服务器上时,我希望它是一个jpg。

So I have an image upload script. It uploads the image and saves it to the space on the server. What I can't seem to get my head around is say, when the user uploads a .png, by the time it saves on my server i want it to be a jpg.

任何人都可以帮忙解决这个问题,请不要只是指导我另一个问题,因为我还没有任何工作。以下是我的代码示例。

Can anyone help with this, and please don't just direct me to another question as I havent had anything work yet. Here is an example of my code.

$name = addslashes($_FILES['image']['name']);
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); 
$size = $_FILES['image']['size'];
$temp = $_FILES ['image']['tmp_name'];
$error = $_FILES ['image']['error'];

if ($error > 0)
    die("Error uploading file! Code $error.");
else


if ($password == "" || $size > 2000000) {
    move_uploaded_file($temp, $images.$name);   
    mysql_query("INSERT INTO image_approval VALUES ('','$description','','$images$name','',NOW())");

    echo "Upload complete!";
    }else{
echo "Error uploading file";
    }


推荐答案

使用 GD ,并假设 $ images 是目录存储图像的位置(带斜杠)和 $ name - 原始图像的文件名:

Using GD, and assuming $images is the directory where you store your images (with ending slash), and $name - the file name of the original image:

$destinationPath = $images . basename($name, $ext) . '.jpg';
$source = imagecreatefrompng($images . $name);
imagejpeg($source, $destinationPath, 75);
imagedestroy($source);

Imagick

$image = new Imagick($images . $name);
$image->writeImage($destinationPath);
$image->destroy();

这篇关于在将服务器png / gif保存到jpg之前转换DURING上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆