[英]I need to compress the thumb images | PHP
Hello Stackoverflowers,你好 Stackoverflowers,
I have successfully been able to compress the main uploaded image, but I don't know how to use the same function to compress the thumbnail.我已经成功地能够压缩主上传的图像,但是我不知道如何使用相同的 function 来压缩缩略图。
The code main purpose is as follows:代码主要用途如下:
1- Upload an image 1-上传图片
2- Rename 2-重命名
3- Compress 3-压缩
4- Create thumbnail 4- 创建缩略图
5- Compress the thumbnail 5-压缩缩略图
My issue it with number 5, I don't know how to compress the thumbnail.我的问题是 5,我不知道如何压缩缩略图。
Below are my php and HTML code:下面是我的 php 和 HTML 代码:
if (isset($_POST["submit"])) {
if (is_array($_FILES)) {
if ($_FILES['image']['size'] <= 5000000) {
$file = $_FILES['image']['tmp_name'];
$sourceProperties = getimagesize($file);
$fileNewName = "[" . "999" . "]-" . time();
$folderPath = "uploads/";
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$imageType = $sourceProperties[2];
$data = getimagesize($file);
$width = $data[0];
$height = $data[1];
switch ($imageType) {
case IMAGETYPE_PNG:
$imageResourceId = imagecreatefrompng($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagepng($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
break;
case IMAGETYPE_GIF:
$imageResourceId = imagecreatefromgif($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagegif($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
break;
case IMAGETYPE_JPEG:
$imageResourceId = imagecreatefromjpeg($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagejpeg($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
break;
default:
echo "Invalid Image type.";
exit;
break;
}
compressImage($file, $folderPath . $fileNewName . "." . $ext, 60);
echo "Image Resize Successfully.";
} else {
echo "too big!";
}
}
}
// Compress image
function compressImage($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);
}
function imageResize($imageResourceId, $width, $height)
{
$targetWidth = (20 / 100) * $width;
$targetHeight = (20 / 100) * $height;
echo $targetWidth . " x " . $targetHeight . " <br> ($width x $height)";
$targetLayer = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
return $targetLayer;
}
HTML HTML
<div class="container">
<form method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Submit" />
</form>
</div>
<br><br>
<a href="index-2.php">Reload Page</a>
Please forgive my silly question as I believe it is a very simple question, but I just started developing using php, just few months ago.请原谅我的愚蠢问题,因为我认为这是一个非常简单的问题,但几个月前我才开始使用 php 进行开发。
Thank you in advance and I really appreciate your help.提前感谢您,我非常感谢您的帮助。
This sounds like an XY-Problem
;这听起来像一个
XY-Problem
; Because all you are doing is chaging the quality.因为你所做的只是改变质量。
If you need low quality thumbnail-image, then set that setting directly while creating thumbnail-image, there is no need to create yet another file.如果您需要低质量的缩略图,则在创建缩略图时直接设置该设置,无需创建另一个文件。
But if you insist on going on, then simply replace original thumbnail-image with the one created by compressImage
function, like:但是,如果您坚持继续,那么只需将原始缩略图替换为
compressImage
function 创建的缩略图,例如:
$newFilePath = $folderPath . $fileNewName . "." . $ext;
compressImage($file, $newFilePath, 60);
// Replace.
unlink($file);
rename($newFilePath, $file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.