我已经实现了这种方法(通过以下php教程)来创建图像的预览:
function createPreview($image_path, $filename) {
header('Content-Type: image/jpeg');
$thumb = imagecreatetruecolor(350, 350);
$source = imagecreatefromjpeg($image_path);
list($width, $height) = getimagesize($image_path);
imagecopyresized($thumb, $source, 0, 0, 0, 0, 350, 350, $width, $height);
imagejpeg($thumb, $filename."_prev.jpg");
}
但我注意到缩放图像会失去很多质量。如何保持缩放图像的质量(我不能使用想象力,我的服务器不支持它)
答案 0 :(得分:6)
imagejpeg默认使用75个品质。所以你需要明确定义它。
imagejpeg($thumb, $filename."_prev.jpg", 100);
另外,请使用imagecopyresampled
imagecopyresampled()将一个图像的矩形部分复制到 另一个图像,平滑地插值像素值,以便在 特别是,减小图像的大小仍然保留很多 清晰度。
答案 1 :(得分:3)
使用imagecopyresampled
代替imagecopyresized
(它是相同的参数,所以只需更改功能):)
答案 2 :(得分:1)
使用PHP Image Magician调整图像大小 http://phpimagemagician.jarrodoberto.com/
答案 3 :(得分:-1)
在PHP代码下面调整图像大小而不会丢失质量以及调整大小的图像将根据宽高比适合上述调整大小的宽度和高度。
从一个文件夹(img)读取图像的代码,并将相同图像的大小调整为所需的宽度和高度,调整为另一个提到的文件夹(resizedImage)。
希望它会有所帮助。
由于
<?php
function resizeImage($SrcImage,$DestImage, $thumb_width,$thumb_height,$Quality)
{
list($width,$height,$type) = getimagesize($SrcImage);
switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/gif':
$NewImage = imagecreatefromgif($SrcImage);
break;
case 'image/png':
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/jpeg':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
default:
return false;
break;
}
$original_aspect = $width / $height;
$positionwidth = 0;
$positionheight = 0;
if($original_aspect > 1) {
$new_width = $thumb_width;
$new_height = $new_width/$original_aspect;
while($new_height > $thumb_height) {
$new_height = $new_height - 0.001111;
$new_width = $new_height * $original_aspect;
while($new_width > $thumb_width) {
$new_width = $new_width - 0.001111;
$new_height = $new_width/$original_aspect;
}
}
} else {
$new_height = $thumb_height;
$new_width = $new_height/$original_aspect;
while($new_width > $thumb_width) {
$new_width = $new_width - 0.001111;
$new_height = $new_width/$original_aspect;
while($new_height > $thumb_height) {
$new_height = $new_height - 0.001111;
$new_width = $new_height * $original_aspect;
}
}
}
if($width < $new_width && $height < $new_height){
$new_width = $width;
$new_height = $height;
$positionwidth = ($thumb_width - $new_width) / 2;
$positionheight = ($thumb_height - $new_height) / 2;
}elseif($width < $new_width && $height > $new_height){
$new_width = $width;
$positionwidth = ($thumb_width - $new_width) / 2;
$positionheight = 0;
}elseif($width > $new_width && $height < $new_height){
$new_height = $height;
$positionwidth = 0;
$positionheight = ($thumb_height - $new_height) / 2;
} elseif($width > $new_width && $height > $new_height){
if($new_width < $thumb_width) {
$positionwidth = ($thumb_width - $new_width) / 2;
} elseif($new_height < $thumb_height) {
$positionheight = ($thumb_height - $new_height) / 2;
}
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
/********************* FOR WHITE BACKGROUND *************************/
//$white = imagecolorallocate($thumb, 255,255,255);
//imagefill($thumb, 0, 0, $white);
if(imagecopyresampled($thumb, $NewImage,$positionwidth, $positionheight,0, 0, $new_width, $new_height, $width, $height)) {
if(imagejpeg($thumb,$DestImage,$Quality)) {
imagedestroy($thumb);
return true;
}
}
}
function resize($source,$destination,$newWidth,$newHeight)
{
ini_set('max_execution_time', 0);
$ImagesDirectory = $source;
$DestImagesDirectory = $destination;
$NewImageWidth = $newWidth;
$NewImageHeight = $newHeight;
$Quality = 100;
$imagePath = $ImagesDirectory;
$destPath = $DestImagesDirectory;
$checkValidImage = getimagesize($imagePath);
if(file_exists($imagePath) && $checkValidImage)
{
if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
echo " --> ".$source.' --> Resize Successful!<BR><BR>';
else
echo " --> ".$source.' --> Resize Failed!<BR><BR>';
}
}
function getDirContents($filter = '', &$results = array()) {
// Source FOLDER
$files = scandir($_SERVER['DOCUMENT_ROOT'].'/imageresize/img/');
$fileCount = 1;
foreach($files as $key => $value){
$ext = explode(".",$value);
$fname = $ext[0].round(microtime(true)*1000);
$filename = $fname.".".$ext[1];
// Source PATH
$path = realpath($_SERVER['DOCUMENT_ROOT'].'/imageresize/img/'.$value);
if(!is_dir($path)) {
if(empty($filter) || preg_match($filter, $path)){
echo "Image # ".$fileCount;
$results[] = $path;
// Destination PATH
$destination = $_SERVER['DOCUMENT_ROOT'].'/imageresize/resizedImage/'.$value;
// Change the desired "WIDTH" and "HEIGHT"
$newWidth = 400; // Desired WIDTH
$newHeight = 350; // Desired HEIGHT
resize($path,$destination,$newWidth,$newHeight);
$fileCount++;
}
} elseif($value != "." && $value != "..") {
getDirContents($path, $filter, $results);
}
}
return $results;
}
getDirContents();
exit;
?>