كيفيت بد عكس بعز ريسايز شدن

Mohammadreza

Well-Known Member
سلام دوستان
من از اين كد براي ساختن يك فايل كوچك تر از فايل اصلي همزمان در آپلود استفاده كردم !
ولي كيفيت عكسي كه ريسايز شد خيلي پايين اومد !
چاره چيه ؟
ممنون !
PHP:
if(isset($_POST['upload']))
{
	$userfile = $_FILES['pic']['tmp_name'];
	$userfile_name = $_FILES['pic']['name'];
	@ copy($userfile,$userfile_name);
	$Src = $userfile_name;
	$newWidth = 400;
	$newHeight = 300;

	header('Content-type: image/jpeg');

	$Thumb = imagecreatetruecolor($newWidth, $newHeight);
	$Source = imagecreatefromjpeg($Src);
	list($Width, $Height) = GetImageSize($Src);
	imagecopyresized($Thumb, $Source, 0, 0, 0, 0, $newWidth, $newHeight, $Width, $Height);

	imagejpeg($Thumb, "th.jpg");
}
مطمئنم كه راهي هست ...
چون همون عكسي كه قبلا جاي ديگه تو سايت ديگه ريسايز شده با كيفيت بالا و همين اندازه !
ريسايز كردم ولي كيفيت اين خيلي بد تر شد ! ( عكس اصلي رو ريسايز كردم و با ريسايز اون سايت مقايسه كردم )
 

sunrise

Member
اين تابع رو حدود چهار سال پيش براي آپلودسنتر تصويري که ساختم نوشتم، که علاوه بر اينکه سايز تصوير رو کوچيک مي کنه، مي تونيد حتي روي تصوير متن هم درج کنيد، اميدوارم اين تونه کمکتون کنه.
PHP:
<?php
function thumbnail($cur_dir, $cur_file, $newwidth, $ofilename, $output_dir, $ib, $ffsize, $fname, $immsize, $immres, $immname)
{
	$dir_name = $cur_dir;
	$olddir = getcwd();
	$dir = opendir($dir_name);
	$filename = $cur_file;
	$format='';
	if(preg_match("/.jpg/i", "$filename"))
	{
		 $format = 'image/jpeg';
	}
	if (preg_match("/.gif/i", "$filename"))
	{
		 $format = 'image/gif';
	}
	if(preg_match("/.png/i", "$filename"))
	{
		 $format = 'image/png';
	}
	if($format!='')
	{
		 list($width, $height) = getimagesize($filename);
		 $newheight=$height*$newwidth/$width;
		 switch($format)
		 {
			  case 'image/jpeg':
			  $source = imagecreatefromjpeg($filename);
			  break;
			  case 'image/gif';
			  $source = imagecreatefromgif($filename);
			  break;
			  case 'image/png':
			  $source = imagecreatefrompng($filename);
			  break;
		 }
		 $thumb = imagecreatetruecolor($newwidth,$newheight);
		 imagealphablending($thumb, false);

		 switch($format)
		 {
			  case 'image/jpeg':
			  $source = imagecreatefromjpeg("$filename");
			  break;
			  case 'image/gif';
			  $source = imagecreatefromgif("$filename");
			  break;
			  case 'image/png':
			  $source = imagecreatefrompng("$filename");
			  break;
		 }
	  $original_x=imageSX($source);
	 $original_y=imageSY($source);
	 if ($original_x > $original_y) {
		  $thumb_w=$newwidth;
		  $thumb_h=$original_y*($newwidth/$original_x);
	 }
	 if ($original_x < $original_y) {
		  $thumb_w=$original_x*($newwidth/$original_y);
		  $thumb_h=$newwidth;
	 }
	 if ($original_x == $original_y) {
		  $thumb_w=$newwidth;
		  $thumb_h=$newwidth;
	 }
	 $thumb=ImageCreateTrueColor($thumb_w,$thumb_h);

		 imagecopyresampled($thumb,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);

		 $filename="$output_dir/$ofilename";

		 @imagejpeg($thumb, $filename);
	}
		 $im = ImageCreatefromjpeg($filename);
// Create some colors
		 $white = imagecolorallocate($thumb, 255, 255, 255);
		 $grey = imagecolorallocate($thumb, 128, 128, 128);
		 $black = imagecolorallocate($thumb, 0, 0, 0);

// The text to draw
		 if ($immsize == 1) {
					$more = true;
					$text = $text . $ffsize.' kb';}

		 if ($immres == 1) {
					if ($more == true) {$text = $text .' - ';}
					$text = $text . $width . 'x' . $height;}
					$more = true;
		 $x = strlen($text);
		 $x = (round($newwidth / 2)) - (round($x / 2))- $x*1.8;
		 $x2 = strlen($fname);
		 $x2 = (round($newwidth / 2)) - (round($x2 / 2))- $x2*1.8;
// Replace path by your own font path
		 $font = 'arial.ttf';

// Add image border
	  if ($ib != null) {
		 imagerectangle($im,0,0,$thumb_w-1,$thumb_h-1,$black);}

// Add Comments
	  if ($newwidth == 150) {$ttss = $thumb_h-$thumb_h / 7;}
	  if ($newwidth == 100) {$ttss = $thumb_h-$thumb_h / 5;}
	  if ($newwidth == 50) {$ttss = $thumb_h-$thumb_h / 2+4;}
	  if ($immsize == 1 or $immres ==1)
		 {imagefilledrectangle($im,0,$ttss,$newwidth,$thumb_h,$black);
			if ($immname == 1) {imagefilledrectangle($im,0,$ttss-11,$newwidth,$thumb_h,$black);}
		 }
	  if ($immsize == 1 or $immres ==1 or $immname == 1) {
			 imagefilledrectangle($im,0,$ttss,$newwidth,$thumb_h,$black);
		  }

// Add the text
		 imagettftext($im, 8, 0, $x, $thumb_h-5, $white, $font, $text);
	  if ($immsize != 1 and $immres !=1 and $immname == 1) {
			 imagettftext($im, 8, 0, $x2, $thumb_h-5, $white, $font, $fname);
		  }
			 else
		  {
			 if ($immname == 1) {
				imagettftext($im, 8, 0, $x2, $thumb_h-5-12, $white, $font, $fname);}
		  }
		 @imagejpeg($im, $filename);
}
?>

براي قطعه کد خودتون هم اگه به جاي تابع imagecopyresized از تابع imagecopyresampled استفاده کنيد فکر کنم مشکل حل بشه.
 

Mohammadreza

Well-Known Member
با اين كد كارم راه افتاد خدا رو شكر ! :x
مي زارم دوستان اگه كسي نياز داشت استفاده كنه ! :x
PHP:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>
 

جدیدترین ارسال ها

بالا