tags:

views:

74

answers:

2

Hi all; In a folder i have my main images ,i need some code in php that read all images from folder and resize it without squash or strech with php and put the resized images in a destination folder.

Thanks

A: 

Resize both width and height by a percent:

newwidth = width * percent%

newheight = height * percent%

If you need a given width newwidth for example, then calculate the percent of newwidth/width*100, and calculate the height based on the resulting percent, as above.

Flavius
+2  A: 

Open your image with ImageCreateFromJPEG, create a new empty image with ImageCreateTrueColor and copy the content with ImageCopyResampled from the original image to the new image. You can save it then with imageJPEG. Like this:

<?php
$imageInfo   = getImageSize( 'image.jpg' );
$imageWidth  = $imageInfo[0];
$imageHeight = $imageInfo[1];
$thumbWidth  = round( $imageWidth / 2 );
$thumbHeight = round( $imageHeight / 2 );

$gdImage = imageCreateFromJPEG( 'image.jpg' );
$gdThumb = imageCreateTrueColor( $thumbWidth, $thumbHeight ); // thumbnail size here

imageCopyResampled( $gdThumb, $gdImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight );
imageJPEG( $gdThumb, 'image_thumb.jpg', 80 );

imageDestroy( $gdImage );
imageDestroy( $gdThumb );
?>
poke
Thanks,but first i must read from maif folder then write to destination folder, i like that this process be in one script so if i have 500 images in the folder then with executing the codes in destination folder i have 500 resized,if you write this part of code i appreciate.
Kaveh
The problem with doing that many files with one execution is php's maximum execution time which is set to 30s (?) by default. So you won't be able to create all images in one run that way. A good way to solve this is to only do like 5 or 10 files per run and have the script run multiple times (or for example with a cronjob).You can read the files out of one directory using the `readdir` function (see the examples in the manual). I suggest you to create some database or text file first where you store the names of the files and update that one with each run of the script.
poke