resize images with good quality with gd2

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
charlierun
Forum Newbie
Posts: 1
Joined: Mon Jun 04, 2007 6:38 am

resize images with good quality with gd2

Post by charlierun »

Hi Im resizing images with gd2 but the quality that I'm optaining is not what Im after, I will like to resize an image but respecting its quality(I will like the image not to pixel).
I dont know if this possible with gd2 this my code maybe I'm doing something wrong.

Code: Select all

<?php
include ("includes/config.php");
include ("includes/funciones.php");
set_time_limit(0);


if ($_FILES['Filedata']['name']) {
	//$nuevoNombre = time().".jpg";


	$size = 300; // the thumbnail height

	$filedir = 'galeria/'; // the directory for the original image
	$thumbdir = 'galeria/'; // the directory for the thumbnail image
	$prefix = 'big_';
	$prefix1 = 'small_'; // the prefix to be added to the original name

	$maxfile = '2000000';
	$mode = '0666';
	
	$userfile_name = $_FILES['Filedata']['name'];
	$userfile_tmp = $_FILES['Filedata']['tmp_name'];
	$userfile_size = $_FILES['Filedata']['size'];
	$userfile_type = $_FILES['Filedata']['type'];
	
	if (isset($_FILES['Filedata']['name'])) 
	{
		 $prod_img = $filedir.$userfile_name;
	     $nuevoNombre = time();
		 $sufijo=".jpg";

		$prod_img_thumb1 =  $thumbdir.$prefix1.$nuevoNombre.$sufijo;
		move_uploaded_file($userfile_tmp, $prod_img);
		chmod ($prod_img, octdec($mode));
		
		$sizes = getimagesize($prod_img);


			$new_width1 = 151;
			$new_height1 = 132;


		
		
		$destimg1=imagecreatetruecolor($new_width1,$new_height1)
			or die('Problem In Creating image');
		$srcimg1=imagecreatefromjpeg($prod_img)
			or die('Problem In opening Source Image');
		if(function_exists('imagecopyresampled'))
		{
			imagecopyresampled($destimg1,$srcimg1,0,0,0,0,$new_width1,$new_height1,ImageSX($srcimg1),ImageSY($srcimg1))
			or die('Problem In resizing');
		}else{
			imagecopyresized($destimg1,$srcimg1,0,0,0,0,$new_width1,$new_height1,ImageSX($srcimg1),ImageSY($srcimg1))
			or die('Problem In resizing');
		}
		imagejpeg($destimg1,$prod_img_thumb1,90)
		//imagejpeg($destimg,$prod_img_thumb,90)
		//imagejpeg($destimg,"hola",90);
			or die('Problem In saving');
			
		imagedestroy($destimg1);
	}

@unlink($prod_img); 

		
}
					
?>
If someone could give the code a glance it would be nice.
Does anyone knows a better way???
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

imagecopyresampled() attempts to keep the quality. The code you have checks if the function exists before attempting to use it... I've never seen that used before. Remove the conditions and just attempt to use imagecopyresampled. If it doesn't work, then maybe you have an old version of the GD library, or an outdated version of PHP.
Post Reply