Posted By

brownrl on 05/18/13


Tagged

display resize image browser crop


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

IMPGTX


Image Resize on the Fly


 / Published in: PHP
 

URL: http://www.itsgotto.be/cv.php

This is my simple image resizer on the fly thing.

-- WARNING -- Resizing and showing images on the fly could potentially kill your server or make the website slow.

Ideally, this should save the resized image in a cache. However, more often than not this method just works, no one complains, and I don't have to make some writable caching directory thing.

If anyting this could give you a good start on how you really want to handle your images.

  1. <?php
  2. /**
  3.  * @file
  4.  * Describes the image resizing technique
  5.  */
  6.  
  7. /**
  8.  * Here is an example of how to use this function
  9.  *
  10.  * File: i.php
  11.  
  12.   if( $_GET['i'] ) {
  13.   require_once( "function.imageResize.php" );
  14.   $_GET['i'] = str_replace("..", "", $_GET['i']); // needs to be lower
  15.   $_GET['i'] = preg_replace("/^\//", "", $_GET['i']); // can't be hard coded
  16.  
  17.   // set a min default
  18.   $_GET['w'] = intval( $_GET['w'] );
  19.   if( $_GET['w'] <= 0 ) {
  20.   $_GET['w'] = 50;
  21.   }
  22.  
  23.   // set a min default
  24.   $_GET['h'] = intval( $_GET['h'] );
  25.   if( $_GET['h'] <= 0 ) {
  26.   $_GET['h'] = 50;
  27.   }
  28.  
  29.   // call it.
  30.   if( file_exists( $_GET['i'] ) ) {
  31.   imageResize( $_GET['i'] , $_GET['w'] , $_GET['h'] );
  32.   }
  33.   }
  34.  
  35.  *
  36.  * Called HTML: <img src="i.php?i=somefile_on_the_sever.jpg&width=500&height=400" />
  37.  *
  38.  */
  39.  
  40.  
  41. /**
  42.  * Resize an Image
  43.  *
  44.  * Output it to the browser.
  45.  *
  46.  * Does not cache, probably should. Maybe version 2.
  47.  */
  48. function imageResize( $source_image, $width = 200, $height = 150, $quality = 70, $crop = true )
  49. {
  50.  
  51. // If the source image isn't an image seriously we got issues
  52. if( ! $image_data = getimagesize( $source_image ) )
  53. {
  54. die( "Whoa can't get the image size of the original?" );
  55. return false;
  56. }
  57.  
  58. // Is it a gif, jpg or png. Sorry anything else probably not worth it.
  59. switch( $image_data['mime'] )
  60. {
  61. case 'image/gif':
  62. $get_func = 'imagecreatefromgif';
  63. $suffix = ".gif";
  64. break;
  65. case 'image/jpeg';
  66. $get_func = 'imagecreatefromjpeg';
  67. $suffix = ".jpg";
  68. break;
  69. case 'image/png':
  70. $get_func = 'imagecreatefrompng';
  71. $suffix = ".png";
  72. break;
  73. }
  74.  
  75.  
  76. // Setup some variables
  77. $img_original = call_user_func( $get_func, $source_image );
  78. $old_width = $image_data[0];
  79. $old_height = $image_data[1];
  80. $new_width = $width;
  81. $new_height = $height;
  82. $src_x = 0;
  83. $src_y = 0;
  84. $current_ratio = round( $old_width / $old_height, 2 );
  85. $desired_ratio_after = round( $width / $height, 2 );
  86. $desired_ratio_before = round( $height / $width, 2 );
  87.  
  88. // Some people don't want to upscale images. I don't care
  89. // Uncomment if you want crash out and not upscale.
  90. /*
  91.   if( $old_width < $width || $old_height < $height )
  92.   {
  93.   // The desired image size is bigger than the original image.
  94.   // Best not to do anything at all really.
  95.   return false;
  96.   }
  97.   */
  98.  
  99.  
  100.  
  101.  
  102. /**
  103.   * If the crop option is left on, it will take an image and best fit it
  104.   * so it will always come out the exact specified size.
  105.   */
  106. if( $crop )
  107. {
  108. /**
  109.   * create empty image of the specified size
  110.   */
  111. $new_image = imagecreatetruecolor( $width, $height );
  112.  
  113. /**
  114.   * Landscape Image
  115.   */
  116. if( $current_ratio > $desired_ratio_after )
  117. {
  118. $new_width = $old_width * $height / $old_height;
  119. }
  120.  
  121. /**
  122.   * Nearly square ratio image.
  123.   */
  124. if( $current_ratio > $desired_ratio_before && $current_ratio < $desired_ratio_after )
  125. {
  126. if( $old_width > $old_height )
  127. {
  128. $new_height = max( $width, $height );
  129. $new_width = $old_width * $new_height / $old_height;
  130. }
  131. else
  132. {
  133. $new_height = $old_height * $width / $old_width;
  134. }
  135. }
  136.  
  137. /**
  138.   * Portrait sized image
  139.   */
  140. if( $current_ratio < $desired_ratio_before )
  141. {
  142. $new_height = $old_height * $width / $old_width;
  143. }
  144.  
  145. /**
  146.   * Find out the ratio of the original photo to it's new, thumbnail-based size
  147.   * for both the width and the height. It's used to find out where to crop.
  148.   */
  149. $width_ratio = $old_width / $new_width;
  150. $height_ratio = $old_height / $new_height;
  151.  
  152. /**
  153.   * Calculate where to crop based on the center of the image
  154.   */
  155. $src_x = floor( ( ( $new_width - $width ) / 2 ) * $width_ratio );
  156. $src_y = round( ( ( $new_height - $height ) / 2 ) * $height_ratio );
  157. }
  158. /**
  159.   * Don't crop the image, just resize it proportionally
  160.   */
  161. else
  162. {
  163. if( $old_width > $old_height )
  164. {
  165. $ratio = max( $old_width, $old_height ) / max( $width, $height );
  166. }else{
  167. $ratio = max( $old_width, $old_height ) / min( $width, $height );
  168. }
  169.  
  170. $new_width = $old_width / $ratio;
  171. $new_height = $old_height / $ratio;
  172.  
  173. $new_image = imagecreatetruecolor( $new_width, $new_height );
  174. }
  175.  
  176. /**
  177.   * Where all the real magic happens
  178.   */
  179. imagecopyresampled( $new_image, $img_original, 0, 0, $src_x, $src_y, $new_width, $new_height, $old_width, $old_height );
  180.  
  181. /**
  182.   * Save it as a JPG File with our $destination_filename param.
  183.   */
  184. header('Content-Type: image/jpeg');
  185. imagejpeg( $new_image, NULL, $quality );
  186.  
  187. /**
  188.   * Destroy the evidence!
  189.   */
  190. imagedestroy( $new_image );
  191. imagedestroy( $img_original );
  192.  
  193. /**
  194.   * Return true because it worked and we're happy. Let the dancing commence!
  195.   */
  196. return true;
  197. }

Report this snippet  

You need to login to post a comment.