Reputation: 10078
i've implement this method (by following php tutorial) for create preview of an images:
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");
}
but i've noticed that scaled image loss a lot of quality. How can i preserve quality of scaled image (i can't use imagick, my server doesn't support it)
Upvotes: 7
Views: 26222
Reputation: 128
Below the PHP code that resize the image without losing the quality as well as the resized image will fit into the mentioned resize width and height as per the aspect ratio.
The code that reads the image from one folder(img) and resized the same image to desired width and height to another mentioned folder(resizedImage).
Hope it will help.
Thanks
<?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;
?>
Upvotes: -2
Reputation: 387
Use imagecopyresampled
instead of imagecopyresized
(it's the same arguments so just change the function) :)
Upvotes: 7
Reputation: 15464
imagejpeg uses 75 quality by default. So you need to define it explicitly.
imagejpeg($thumb, $filename."_prev.jpg", 100);
Also, use imagecopyresampled
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.
Upvotes: 7