【发布时间】:2019-11-19 16:15:55
【问题描述】:
在将图像上传到文件夹之前,您将如何压缩图像大小。到目前为止,我的代码低于我的代码,我将如何压缩图像?谢谢!
$temp = explode(".", $_FILES["Image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
echo $newfilename;
if (!!$_FILES['Image']['tmp_name']) {
$info = explode('.', strtolower($_FILES['Image']['name']));
if (in_array( end($info), $allow))
if(move_uploaded_file( $_FILES['Image']['tmp_name'], $todir . $newfilename ) ) {
echo "success";
}
}
else {
echo "error";
}
我找到了这段代码,但我不确定如何用我的代码实现它。
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
$source_img = 'source.jpg';
$destination_img = 'destination .jpg';
$d = compress($source_img, $destination_img, 90);
【问题讨论】:
-
PHP 是服务器端,所以文件必须先上传到服务器,然后 PHP 才能操作文件。如果您的意思是在移动到上传文件夹之前进行压缩:stackoverflow.com/questions/11418594/…
-
您也可以使用php intervention,它易于使用,您可以使用此库进行压缩、裁剪、过滤等操作。
-
@catcon 的问题是我不知道如何通过我的代码设置来实现它。我需要帮助了解如何压缩它然后插入它,反之亦然
标签: php image compression