Link to home
Start Free TrialLog in
Avatar of MOSTAGHASSI
MOSTAGHASSIFlag for United States of America

asked on

Resize image withour squash or strech with php?

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  and put the resized images in a destination folder.

Thanks
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

There are a lot of different options when you're resizing like that. Check out phpThumb:
http://phpthumb.sourceforge.net/

It should take care of that type of thing for you, and there are probably examples of this type of situation you can use.
You need to use the DIR class to scan through your source directory, looking for valid image names. You can then use the IMAGECOPYRESIZED function to resize your images to your specifications and write it back to your destination directory.

You could also use the Verto upload class to resize images. It does other things too and it will work with local files just as easily as uploaded files.

http://www.verot.net/php_class_upload.htm
Avatar of MOSTAGHASSI

ASKER

Thanks, i downloaded the codes from verot.net ,but how can i use it i'm not professentiol with php ,
my source directory =mainimage
destination directory=resized
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
bportlock;
Thanks, in next hour i test your codes and back to you.
I tested the code but no result my condition is

In folder of "artandculture" i have artandculture.php that put your codes in addition inside this folder there are 2 folders:
testmainimage
testresizedimage

and 2 line of codes changed as below:

$imageFolder = "/artandculture/testmainimage";
$newFilePath = "/artandculture/testresizedimage";
I doubt that the paths you are using are correct


$imageFolder = "/artandculture/testmainimage";
$newFilePath = "/artandculture/testresizedimage";

since I know of no machines with a top level folder called "artandculture".

Either make them relative paths by prefixing with a dot or full paths starting at the file systemm root like so


$imageFolder = "./artandculture/testmainimage";
$newFilePath = "./artandculture/testresizedimage";

or something more like this

$imageFolder = "/var/www/htdocs/artandculture/testmainimage";
$newFilePath = "/var/www/htdocs/artandculture/testresizedimage";

This second example depends on the exact location of your web root. The code also assumes that the GD library is installed in your version of PHP
I test it on my laptop.
How can i know that GD library is installed?
My php is 5.2.5
after testing:

<?php
phpinfo();
?>

then i saw that gd library is not installed.
Where can i find GD library for this ver of php?
thanks
It depends on your operating system. If you are using SuSE Linux then you can look for php5-gd in YaST. If on Ubuntu then

sudo apt-get install php5-gd

Apparently under Windows (and I do not use Windows, so I'm a bit unsure about this) edit php.ini and add or enable this line

extension=php_gd2.dll

in the extensions section.
I use winxp+iis5
I cannot advise you on IIS as I know nothing about it, however I would have thought that the PHP settings in php.ini are independent of IIS. The changes I outlined above were for PHP not Apache so they should be separate from IIS as well

Try changing the "extension section" as I outlined above and, to be on the safe side,  restart IIS after saving your changes.

From what I understand the php5 GD dll is probably on your machine somewhere. If it is not you may need to post a separate question under PHP for Windows.
You do not have to have GD2 installed to resize images.  Just choose the older version of the GD functions.  This code snippet will show you how you can do that.  But if you do not have any GD installed at all, choose the latest version - 2+, IIRC.
// A FUNCTION TO DETERMINE IF GD IS AT LEVEL 2 OR MORE
function get_gd_info($display=FALSE)
{
 
// IS GD INSTALLED AT ALL?
   if (!function_exists("gd_info"))
   {
      if ($display) echo "<br/>GD NOT INSTALLED\n";
      return FALSE;
   }
 
// IF GD IS INSTALLED GET DETAILS
   $gd = gd_info();
 
// IF DISPLAY IS REQUESTED, PRINT DETAILS
   if ($display)
   {
      echo "<br/>GD DETAILS:\n";
      foreach ($gd as $key => $value)
      {
         if ($value === TRUE)  $value = 'YES';
         if ($value === FALSE) $value = 'NO';
         echo "<br/>$key = $value \n";
      }
   }
 
// RETURN THE VERSION NUMBER
   $gd_version = ereg_replace('[^0-9\.]', '', $gd["GD Version"]);;
   return $gd_version;
}
 
 
// A FUNCTION TO MAKE AN IMAGE INTO THE RIGHT WIDTH FOR PAGE DISPLAY
// WILL WORK IF GD2 NOT INSTALLED, BUT WILL MAKE BETTER IMAGES WITH GD2
// INPUT IS THE IMAGE FILE NAME, OUTPUT IS AN IMAGE RESOURCE, OR FALSE IF NO RESIZE NEEDED
function create_right_size_image($image, $width=720)
{
// IS GD HERE?
   $gdv = get_gd_info();
   if (!$gdv) return FALSE;
 
// GET AN IMAGE THING
   $source = imagecreatefromjpeg("$image");
 
// GET THE X AND Y DIMENSIONS
   $imageX = imagesx($source);
   $imageY = imagesy($source);
 
// IF NO RESIZING IS NEEDED
   if ($imageX <= $width)
   {
      return FALSE;
   }
 
// THE WIDTH IS TOO GREAT - MUST RESIZE
   $tnailX = $width;
   $tnailY = (int) (($tnailX * $imageY) / $imageX );
 
// WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
   if ($gdv >= 2)
   {
// IF GD IS AT LEVEL 2 OR ABOVE
      $target = imagecreatetruecolor($tnailX, $tnailY);
      imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
   } else
   {
// IF GD IS AT A LOWER REVISION LEVEL
      $target = imagecreate($tnailX, $tnailY);
      imagecopyresized   ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
   }
   return $target ;
}

Open in new window

Ray;
I checked php.ini  extension=php_gd2.dll is exist and i removed its (;) and restarted iis then i put this part of your codes and execute it but there are no output on my page regarding gd for any version ,does it mean that i don't have gd or i have mistake for impelementation of code?

<?php
// A FUNCTION TO DETERMINE IF GD IS AT LEVEL 2 OR MORE
function get_gd_info($display=FALSE)
{
 
// IS GD INSTALLED AT ALL?
   if (!function_exists("gd_info"))
   {
      if ($display) echo "<br/>GD NOT INSTALLED\n";
      return FALSE;
   }
 
// IF GD IS INSTALLED GET DETAILS
   $gd = gd_info();
 
// IF DISPLAY IS REQUESTED, PRINT DETAILS
   if ($display)
   {
      echo "<br/>GD DETAILS:\n";
      foreach ($gd as $key => $value)
      {
         if ($value === TRUE)  $value = 'YES';
         if ($value === FALSE) $value = 'NO';
         echo "<br/>$key = $value \n";
      }
   }
 
// RETURN THE VERSION NUMBER
   $gd_version = ereg_replace('[^0-9\.]', '', $gd["GD Version"]);;
   return $gd_version;
   echo "$gd_version";
}

?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I RUN phpinfo() but i don't see any part regarding GD.
Hmm... That's a pretty clear signal - those outputs are in alphabetical order, IIRC.  You may want to reinstall PHP to be sure you get the parts you need.  Sorry there is not any quick fix that I know of, ~Ray
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, and back up your php.ini file before following my steps on #2.
gr8gonzo;
I checked  the , Loaded Configuration File =c:\windows\php.ini and this is the file that i removed its semicolom for extention(extension=php_gd2.dll) ,ofcourse on c:\php\php.ini  there is php.ini file(i haven't install in programm file).

-Restart is ok and it isn't stop start,and take around 30s.

-The main zip file for php is :
php-5.2.5-Win32.zip    (9.7MB)

Please let me know that if the gd library installed then in phpinfo() where can see it?
and does it need that download php again and install it?

-There is not another simple software that scan a dir and resize its .jpg and put in a folder?
i have firework but the quality of rezised image is not good.

thanks
It seems you have 2 php.ini files. When you run phpinfo() do the following.

When PHPINFO displays, look at the 5th and 6th boxes from the top. One is labelled "Configuration File" and tells you which php.ini is in use and the next box under it should be "Scan this dir for additional .ini files". Make a note of it, although chances are you will not need this.

Scroll down. phpinfo reports major sections in alphabetical order. About halfway down (under G) there should be a section called GD. Scan for "GD Support". if you cannot find it then you lack the GD libraries. If they are missing then edit the php.ini file that you noted down in the first step and ensure that this has the line

extension=php_gd2.dll
bportlock;
In second box its name is :
Configuration
PHP Core

there is not regarding php.ini, and there is not box for "Scan this dir for additional .ini files" but there is box fo gd and inside it ->GD Support =enabled and GD Version =bundled (2.0.34 compatible)



extension=php_gd2.dll    is ok also.
It sounds like your second box is set up with GD2 installed but your first box is not.
second box=Configuration PHP Core      has not anythings about Gd2
but the box of gd  which is 11th is about gd.
Ray;
Sorry ,i was wrong for GD ,as i wrote in my previous comment there is GD box ,i don't know that why before i could not see but i think that it was not effect by restart iis and after restarting of laptop it caused to appear.