/ Published in: 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.
-- 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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * @file * Describes the image resizing technique */ /** * Here is an example of how to use this function * * File: i.php if( $_GET['i'] ) { require_once( "function.imageResize.php" ); $_GET['i'] = str_replace("..", "", $_GET['i']); // needs to be lower $_GET['i'] = preg_replace("/^\//", "", $_GET['i']); // can't be hard coded // set a min default $_GET['w'] = intval( $_GET['w'] ); if( $_GET['w'] <= 0 ) { $_GET['w'] = 50; } // set a min default $_GET['h'] = intval( $_GET['h'] ); if( $_GET['h'] <= 0 ) { $_GET['h'] = 50; } // call it. if( file_exists( $_GET['i'] ) ) { imageResize( $_GET['i'] , $_GET['w'] , $_GET['h'] ); } } * * Called HTML: <img src="i.php?i=somefile_on_the_sever.jpg&width=500&height=400" /> * */ /** * Resize an Image * * Output it to the browser. * * Does not cache, probably should. Maybe version 2. */ function imageResize( $source_image, $width = 200, $height = 150, $quality = 70, $crop = true ) { // If the source image isn't an image seriously we got issues { return false; } // Is it a gif, jpg or png. Sorry anything else probably not worth it. switch( $image_data['mime'] ) { case 'image/gif': $get_func = 'imagecreatefromgif'; $suffix = ".gif"; break; case 'image/jpeg'; $get_func = 'imagecreatefromjpeg'; $suffix = ".jpg"; break; case 'image/png': $get_func = 'imagecreatefrompng'; $suffix = ".png"; break; } // Setup some variables $old_width = $image_data[0]; $old_height = $image_data[1]; $new_width = $width; $new_height = $height; $src_x = 0; $src_y = 0; // Some people don't want to upscale images. I don't care // Uncomment if you want crash out and not upscale. /* if( $old_width < $width || $old_height < $height ) { // The desired image size is bigger than the original image. // Best not to do anything at all really. return false; } */ /** * If the crop option is left on, it will take an image and best fit it * so it will always come out the exact specified size. */ if( $crop ) { /** * create empty image of the specified size */ /** * Landscape Image */ if( $current_ratio > $desired_ratio_after ) { $new_width = $old_width * $height / $old_height; } /** * Nearly square ratio image. */ if( $current_ratio > $desired_ratio_before && $current_ratio < $desired_ratio_after ) { if( $old_width > $old_height ) { $new_width = $old_width * $new_height / $old_height; } else { $new_height = $old_height * $width / $old_width; } } /** * Portrait sized image */ if( $current_ratio < $desired_ratio_before ) { $new_height = $old_height * $width / $old_width; } /** * Find out the ratio of the original photo to it's new, thumbnail-based size * for both the width and the height. It's used to find out where to crop. */ $width_ratio = $old_width / $new_width; $height_ratio = $old_height / $new_height; /** * Calculate where to crop based on the center of the image */ } /** * Don't crop the image, just resize it proportionally */ else { if( $old_width > $old_height ) { }else{ } $new_width = $old_width / $ratio; $new_height = $old_height / $ratio; } /** * Where all the real magic happens */ imagecopyresampled( $new_image, $img_original, 0, 0, $src_x, $src_y, $new_width, $new_height, $old_width, $old_height ); /** * Save it as a JPG File with our $destination_filename param. */ /** * Destroy the evidence! */ /** * Return true because it worked and we're happy. Let the dancing commence! */ return true; }
URL: http://www.itsgotto.be/cv.php