Mark Forums Read
  #1  
Old 08-25-2008, 10:29 AM
Sakari Sakari is offline
Member
 
Join Date: Jul 2008
Posts: 34
Sakari is on a distinguished road
Default PHP resizer

Hi



Resizing an image in PHP and maintaining its aspect ratio
________________________________________
A question that has been asked many times is how to resize an image in PHP while maintaining the aspect ration of the original image.

The function below does just that, it takes an image resource and the new dimensions for the image in pixels and returns the resized image.
Quote:
Code:
/**
* Resize and image while maintaining its aspect ratio.
*
* @param resource $src The image to resize.
* @param int $w The target width.
* @param int $h The target height.
* @return resource The resized image or the original image if it did not need to be scaled.
*/
function resizeImage($src, $w, $h) {

// Get the current size
$width = ImageSx($src);
$height = ImageSy($src);

// If one dimension is right then nothing to do
if($width == $w || $height == $h)
return($src);

// Calculate new size
if(($w - $width) > ($h - $height)) { // use height
$s = $h / $height;
$nw = round($width * $s);
$nh = round($height * $s);
}
else { // Use width
$s = $w / $width;
$nw = round($width * $s);
$nh = round($height * $s);
}

// Resize to correct size
$im = ImageCreateTrueColor($nw, $nh);
ImageCopyResampled($im, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);

// Return the new image
return($im);
}
A simple example to load, resize and send an image to the browser would be:
Code:
$image = ImageCreateFromJPEG('images.jpg');
$image = resizeImage($image, 400, 400);
header("Content-Type: image/jpeg");
ImageJPEG($image, '', 80);

thanks
__________________
web design services
Free Templates
Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
  #2  
Old 11-15-2008, 09:36 AM
orlando orlando is offline
Junior Member
 
Join Date: Oct 2008
Posts: 7
orlando is on a distinguished road
Default

hi sakari

its not properly work but some changes make it perfect.

thanks
Reply With Quote
  #3  
Old 03-06-2009, 05:00 AM
vks87 vks87 is offline
Member
 
Join Date: Mar 2009
Posts: 85
vks87 is on a distinguished road
Default

ya absolutely orlando is right.I am fully agreed with orlando you should do that which he said you..
Reply With Quote
  #4  
Old 08-11-2011, 06:00 AM
peterjack15 peterjack15 is offline
Senior Member
 
Join Date: Aug 2011
Posts: 146
peterjack15 is on a distinguished road
Default

ya absolutely orlando is right.I am fully agreed with orlando you should do that which he said you..
Reply With Quote
  #5  
Old 08-26-2011, 08:40 AM
amankumar amankumar is offline
Senior Member
 
Join Date: Jul 2011
Posts: 146
amankumar is on a distinguished road
Default Php resizing the pic

<?php

/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.u...e-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
Reply With Quote
  #6  
Old 08-26-2011, 10:18 AM
aman1 aman1 is offline
Junior Member
 
Join Date: Aug 2011
Posts: 12
aman1 is on a distinguished road
Default Thanks

Thanks for sharing the useful information.
Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
Reply


Thread Tools
Display Modes

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 On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 09:44 AM.


Powered by vBulletin Version 3.6.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 2.4.0