note 69815 deleted from function.imagecopyresampled by colder

From: Date: Fri, 22 Sep 2006 01:36:48 +0000
Subject: note 69815 deleted from function.imagecopyresampled by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117407@lists.php.net to get a copy of this message
Note Submitter: zdenek dot schee at gmail dot com Reason: bad code ---- This is simple function for resizing jpg files... (it used to resize even gif files to gif files (between /* and */) but then i realized that resizing gif files causes much worse the quality than converting to jpg and than resizing - the gif resizing is not modified for general use in the function). The $width is a width of a new file and the $height a height of a new file... if one of these dimensions is the same or lower than in source image the file will not be resized to bigger image but just copied. The width/height ratio is kept in the destination file. <?php function resizeit($uploadsource,$destination,$width,$height) { if($uploadsource) { if(file_exists($destination)) unlink($destination); $extension = jpg; // The file $filename = $uploadsource; // Set a maximum height and width manually //$width = 60; //$height = 60; // Content type //header('Content-type: image/jpeg'); // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); // selecting operator OR instead of AND would causes that just one lower dimension (not both of them) would be enough to be just copied - might be useful if(($width_orig <= $width) and ($height_orig <= $height)) { copy($filename, $destination); } else { if ($width && ($width_orig < $height_orig)) { $width = ($height / $height_orig) * $width_orig; } else { $height = ($width / $width_orig) * $height_orig; } // Resample $image_p = imagecreatetruecolor($width, $height); /* for case of resizing gif files if($extension == "gif") { //Causes that the background of gif image with no background will be white, not black $bgc = imagecolorallocate ($image_p, 255, 255, 255); $tc = imagecolorallocate ($image_p, 0, 0, 0); imagefilledrectangle ($image_p, 0, 0, $width, $height , $bgc); $image = imagecreatefromgif($filename); } elseif(($extension == "jpg") or ($extension == "jpeg"))*/ $image = imagecreatefromjpeg($uploadsource); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output /* for case of resizing gif files if($extension == "gif") imagegif($image_p, "znacky/".$id."_60.gif", 100); elseif(($extension == "jpg") or ($extension == "jpeg")) */ //you can change the quality from 100 to another integer .. 80 is probably the most common imagejpeg($image_p, $destination, 100); chmod($destination,0777); } } } ?>

« previous php.notes (#117407) next »