Created
August 7, 2013 20:04
-
-
Save morningtoast/6178039 to your computer and use it in GitHub Desktop.
PHP GD resizing, upscaling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function generate_image_thumbnail($source_image_path, $thumbnail_image_path, $max_width, $max_height, $quality=35) { | |
| list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path); | |
| switch ($source_image_type) { | |
| case IMAGETYPE_GIF: | |
| $source_gd_image = imagecreatefromgif($source_image_path); | |
| break; | |
| case IMAGETYPE_JPEG: | |
| $source_gd_image = imagecreatefromjpeg($source_image_path); | |
| break; | |
| case IMAGETYPE_PNG: | |
| $source_gd_image = imagecreatefrompng($source_image_path); | |
| break; | |
| } | |
| if ($source_gd_image === false) { | |
| return false; | |
| } | |
| $source_aspect_ratio = $source_image_width / $source_image_height; | |
| $thumbnail_aspect_ratio = $max_width / $max_height; | |
| if ($thumbnail_aspect_ratio > $source_aspect_ratio) { | |
| $thumbnail_image_width = (int) ($max_height * $source_aspect_ratio); | |
| $thumbnail_image_height = $max_height; | |
| } else { | |
| $thumbnail_image_width = $max_width; | |
| $thumbnail_image_height = (int) ($max_width / $source_aspect_ratio); | |
| } | |
| $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height); | |
| imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height); | |
| imagejpeg($thumbnail_gd_image, $thumbnail_image_path, $quality); | |
| imagedestroy($source_gd_image); | |
| imagedestroy($thumbnail_gd_image); | |
| return true; | |
| } | |
| for ($a=25; $a <= 50; $a++) { | |
| $f = "resize/resize".$a.".jpg"; | |
| generate_image_thumbnail("bigimage.jpg",$f,1280,1280,$a); | |
| echo '<p><img src="'.$f.'" width="640" />'.$a.'</p>'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment