Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Resize images with php
Old 06-26-2006, 12:25 AM Resize images with php
stOx's Avatar
Machine

Latest Blog Post:
Worlds Smallest Car - Peel P50
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
Trades: 0
I'm looking to create a script which when run will go through a directory and resize every image in the directory, and put the resized image in a thumbs dir.

Heres the thing though, I don't want the image to lose any quality because this is for my photography, not just a products page. I also dont want the image to be scaled down to a specific size, I want its dimentions to be a percentage of it's orriginal size to accomodate photos that were taken long ways..

Does anyone know how i would achieve this?

By the way, I have the looping throough the files bit sorted out, I just need to know of a function oor code snippet that will resize an image and not lose any quality.

thanks.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
stOx is offline
Reply With Quote
View Public Profile Visit stOx's homepage!
 
 
Register now for full access!
Old 06-26-2006, 01:34 AM Re: Resize images with php
Average Talker

Posts: 15
Name: Jeffery Mandrake
Location: San Diego
Trades: 0
Here's a function I've used with pretty good results. Resizes jpg's really nicely.
I think I got it from PHP.net's docs...

Code:
/*
 Function createthumb($name,$filename,$new_w,$new_h)
 creates a resized image
 variables:
 $name  Original filename
 $filename Filename of the resized image
 $new_w  width of resized image
 $new_h  height of resized image
*/ 
function createthumb($name,$filename,$new_w,$new_h){
 //global $gd2;
 $system=explode(".",$name);
 if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
 if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
 $old_x=imageSX($src_img);
 $old_y=imageSY($src_img);
 if ($old_x > $old_y) {
  $thumb_w=$new_w;
  $thumb_h=$old_y*($new_h/$old_x);
 }
 if ($old_x < $old_y) {
  $thumb_w=$old_x*($new_w/$old_y);
  $thumb_h=$new_h;
 }
 if ($old_x == $old_y) {
  $thumb_w=$new_w;
  $thumb_h=$new_h;
 }
 //if ($gd2==""){
   //$dst_img=ImageCreate($thumb_w,$thumb_h);
   //imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
 //}else{
  $dst_img=ImageCreateTrueColor($new_w,$new_h);//create canvas picture  
  $thumbBgColor = imagecolorallocate($dst_img, 255, 255, 255);
  imagefilledrectangle($dst_img, 0, 0, $new_w, $new_w, $thumbBgColor);
  $xoffset = 0;
  $yoffset = 0;
  //calculate image offset to center the thumbnail
  if ($new_w > $thumb_w){
   $xoffset = (int) (($new_w - $thumb_w) / 2);
  } 
  if ($new_h > $thumb_h){
   $yoffset = (int) (($new_h - $thumb_h) / 2);
  }
  imagecopyresampled($dst_img,$src_img,$xoffset,$yoffset,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
 //}
 if (preg_match("/png/",$system[1])){
  imagepng($dst_img,$filename); 
 } else {
  imagejpeg($dst_img,$filename); 
 }
 imagedestroy($dst_img); 
 imagedestroy($src_img); 
}
You can see how the thumbs look at this site's product pages - Well Beings. There's a link from the thumbnails to popup a window with the original sized image.

-Jeffery
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

~~~~~~~~~~~~~~~~~~~

Please login or register to view this content. Registration is FREE
cybernewsmaster is offline
Reply With Quote
View Public Profile Visit cybernewsmaster's homepage!
 
Old 06-26-2006, 08:51 AM Re: Resize images with php
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
If you have ImageMagik on your server you can do it much more efficiantly using convert.

Code:
list($width, $height, $type, $attr) = getimagesize($name);
	$height > $width ?
	exec("/usr/bin/convert -size 150x200  {$name}  -thumbnail 75x100 {$filename}")
	:
	exec("/usr/bin/convert -size 220x80  {$name}  -thumbnail 110x40 {$filename}");
I only add this because PHP-GD winges like hell in winter when your dealing with large images. ImageMagik dont care it just does.

I dont know if its available for Windows machines but its certainly packaged with every linux distro.

Ibbo
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 06-26-2006, 04:23 PM Re: Resize images with php
stOx's Avatar
Machine

Latest Blog Post:
Worlds Smallest Car - Peel P50
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
Trades: 0
cheers guys, i'll give them a go tonight.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
stOx is offline
Reply With Quote
View Public Profile Visit stOx's homepage!
 
Reply     « Reply to Resize images with php
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.48057 seconds with 12 queries