رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
جلوگیری از کاهش بیش از حد کیفیت تصاویر با استفاده از کتابخانه GD
#1
سلام.
جلوگیری از کاهش بیش از حد کیفیت تصاویر با استفاده از کتابخانه GD چجوریه؟
من از این کد استفاده میکنم اما متاسفانه خیلی افت کیفیت داره.
کوالیتیم رو 100 میذارم اما باز تاثیر نداره.
کیفیت تصاویر هم قبلش عالیه و بعدش افتضاح.

<?php

/*
*	!!! THIS IS JUST AN EXAMPLE !!!, PLEASE USE ImageMagick or some other quality image processing libraries
*/
$url = Yii::$app->homeUrl;




$imgUrl = Yii::getAlias('@webroot').'/'.str_replace($url,'',$_POST['imgUrl']);
// original sizes
$imgInitW = $_POST['imgInitW'];
$imgInitH = $_POST['imgInitH'];
// resized sizes
$imgW = $_POST['imgW'];
$imgH = $_POST['imgH'];
// offsets
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];
// crop box
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];
// rotation angle
$angle = $_POST['rotation'];

/*
// original sizes
$imgUrl = Yii::getAlias('@webroot').'/'.str_replace($url,'',$_GET['imgUrl']);

$imgInitW = $_GET['imgInitW'];
$imgInitH = $_GET['imgInitH'];
// resized sizes
$imgW = $_GET['imgW'];
$imgH = $_GET['imgH'];
// offsets
$imgY1 = $_GET['imgY1'];
$imgX1 = $_GET['imgX1'];
// crop box
$cropW = $_GET['cropW'];
$cropH = $_GET['cropH'];
// rotation angle
$angle = $_GET['rotation'];
 
 */

$jpeg_quality = 100;

$output_filename = Yii::getAlias('@webroot').'/temp/croppedImg_'.rand();

// uncomment line below to save the cropped image in the same location as the original image.
//$output_filename = dirname($imgUrl). "/croppedImg_".rand();

$what = getimagesize($imgUrl);



switch(strtolower($what['mime']))
{
    case 'image/png':
        $img_r = imagecreatefrompng($imgUrl);
		$source_image = imagecreatefrompng($imgUrl);
		$type = '.png';
        break;
    case 'image/jpeg':
        $img_r = imagecreatefromjpeg($imgUrl);
		$source_image = imagecreatefromjpeg($imgUrl);
		error_log("jpg");
		$type = '.jpeg';
        break;
    case 'image/gif':
        $img_r = imagecreatefromgif($imgUrl);
		$source_image = imagecreatefromgif($imgUrl);
		$type = '.gif';
        break;
    default: die('image type not supported');
}

$name = $url.str_replace(Yii::getAlias('@webroot'), '', $output_filename).$type;

//Check write Access to Directory

if(!is_writable(dirname($output_filename))){
	$response = Array(
	    "status" => 'error',
	    "message" => 'Can`t write cropped File'
    );	
}else{

    // resize the original image to size of editor
    $resizedImage = imagecreatetruecolor($imgW, $imgH);
	imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
    // rotate the rezized image
    $rotated_image = imagerotate($resizedImage, -$angle, 0);
    // find new width & height of rotated image
    $rotated_width = imagesx($rotated_image);
    $rotated_height = imagesy($rotated_image);
    // diff between rotated & original sizes
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;
    // crop rotated image to fit into original rezized rectangle
	$cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
	imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
	imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
	// crop image into selected area
	$final_image = imagecreatetruecolor($cropW, $cropH);
	imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
	imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
	// finally output png image
	//imagepng($final_image, $output_filename.$type,7);
	imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
    $response = Array(
	    "status" => 'success',
	    "url" => $name
    );
}
print json_encode($response);

پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان