对于这个问题的任何帮助,我将不胜感激。我有一个基于 joomla 的网站并使用第三方扩展,我试图让访问者上传包括照片在内的信息。但是,我需要在上传到服务器之前实现一些 php 代码来调整图像大小,以减少上传时间和上传失败。该网站的开发人员已经放置了“调整大小黑客”,但这只会在到达服务器后调整图像大小。
// Resize Params Hack
$OriginalfileWidth = getimagesize($file);
$NewFileWidth = 400; // Set here the new desired width
if ($OriginalfileWidth[0] > $NewFileWidth) {
$this->resize($NewFileWidth, $file, $ext);
}
}
}
}
protected function resize($newWidth, $targetFile, $ext) {
$targetFile = JPATH_ROOT . '/' . $targetFile;
if (empty($newWidth) || empty($targetFile)) {
return false;
}
switch ($ext) {
case 'jpg':
$src = imagecreatefromjpeg($targetFile);
break;
case 'gif':
$src = ImageCreateFromGif($targetFile);
break;
case 'png':
$src = ImageCreateFrompng($targetFile);
break;
}
list($width, $height) = getimagesize($targetFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
switch ($ext) {
case 'jpg':
imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
break;
case 'gif':
imagegif($tmp, $targetFile);
break;
case 'png':
imagepng($tmp, $targetFile, 8);
break;
}
}
}
我已经注意到搜索stackoverflow的代码,但我根本不知道如何输入它。非常感谢您的帮助。马修