GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Apr-2009, 18:43
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

Resize images without stretching or squeezing


PHP Code:

<?php
/*
 * This script demonstrates the mathematics of resizing an image to a different
 * width and height without stretching or squeezing the original image. If the
 * source image has a different aspect ratio than the destination image, the
 * source image will be cropped.
 * 
 * Consider a source image with a width of 320 pixels and a height of 240
 * pixels. The destination image is to be 80 pixels by 80 pixels. If these
 * dimensions were passed directly to imagecopyresampled(), the resized image
 * would appear to have a squeezed width. To prevent this, the width of the
 * source image should be cropped to 240 pixels so that the ratio of its width
 * to its height is 1:1, which is the same as the destination image's ratio.
 * When the source image is resized, it will not appear squeezed.
 */

$src = imagecreatefromjpeg('sample.jpg'); // Source image

$dw = 80;                    // Destination image Width
$dh = 80;                    // Destination image Height

$sw = imagesx($src);         // Source image Width
$sh = imagesy($src);         // Source image Height

$wr = $sw / $dw;             // Width Ratio (source:destination)
$hr = $sh / $dh;             // Height Ratio (source:destination)

$cx = 0;                     // Crop X (source offset left)
$cy = 0;                     // Crop Y (source offset top)

if ($hr < $wr)               // Height is the limiting dimension; adjust Width
{
    $ow = $sw;               // Old Source Width (temp)
    $sw = $dw * $hr;         // New virtual Source Width
    $cx = ($ow - $sw) / 2;   // Crops source width; focus remains centered
}
if ($wr < $hr)               // Width is the limiting dimension; adjust Height
{
    $oh = $sh;               // Old Source Height (temp)
    $sh = $dh * $wr;         // New virtual Source Height
    $cy = ($oh - $sh) / 2;   // Crops source height; focus remains centered
}
// If the width ratio equals the height ratio, the dimensions stay the same.

$dst = imagecreatetruecolor($dw, $dh); // Destination image
imagecopyresampled($dst, $src, 0, 0, $cx, $cy, $dw, $dh, $sw, $sh);

// Previews the resized image (not saved)
header('Content-type: image/jpeg');
imagejpeg($dst);
?>

 
 

Recent GIDBlogReview: Gel laptop cooling pad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading wrong images. Sujith MS Visual C++ / MFC Forum 0 08-May-2007 05:48
Loading several images simultaneously yashin MS Visual C++ / MFC Forum 0 08-Apr-2007 14:03
Scaling Images Richardknox MySQL / PHP Forum 2 01-Dec-2006 11:25
GIM gidedit - a fltk fluid resize project cable_guy_67 FLTK Forum 2 01-Jun-2005 16:00
Why are my images distorted? rhino1616 Graphics Forum 0 27-Jun-2003 10:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.