Image Resizing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neugent
Forum Newbie
Posts: 24
Joined: Wed Jul 06, 2005 3:35 am
Location: Philippines

Image Resizing

Post by neugent »

I have this client that has a directory containing around 1300 images that contains actual and detail pictures of his items. For example i have this NX-VA1-N1 as the actual and NX-VA1-N1-DE as the detail image but also contains some files that has no details at all. And i want to automate their resizing in three categories and be placed in three different subfolders under Images Folder.

Example:

Directory Name: Images
Note: Images at high quality and at 800 x 600 pixels in dimension.
NX-VA1-N1
NX-VA1-N1-DE
NX-VA1-N2
NX-VA1-N3
NX-VA1-N3-DE
NX-VA1-N5
MX-ZA-N10
MX-ZA-N10-DE

I want to resize them into three categories, one as Download Copy, The Site Copy and the Thumbnail Copy and all at a quality of 80%. The PHP file will create the following the subdirectories below and resizes the images according to its instructions, below are the results.

SubDirectory Name: Download Copy
Note: Images at 80% quality and at 398 x 531 pixels in dimension.
NX-VA1-N1
NX-VA1-N1-DE
NX-VA1-N2
NX-VA1-N3
NX-VA1-N3-DE
NX-VA1-N5
MX-ZA-N10
MX-ZA-N10-DE

SubDirectory Name: Site Copy
Note: Images at 80% quality and at 500 x 667 pixels in dimension but doesnt contain the detail copy.
NX-VA1-N1
NX-VA1-N2
NX-VA1-N3
NX-VA1-N5
MX-ZA-N10

SubDirectory Name: Thumbnail
Note: Images at 80% quality and at 75 x 100 pixels in dimension and i want that if a certain item code has a detail copy, it deletes the actual copy and rename the detail in to the actual item code removing the -DE code.
Example: Resize all images. If MX-ZA-N10 has detail copy, delete MX-ZA-N10 then rename MZ-ZA-N10-DE into MX-ZA-N10.
NX-VA1-N1
NX-VA1-N2
NX-VA1-N3
NX-VA1-N5
MX-ZA-N10

Directory Structure
\Images
|-- \Download
|-- \Site
|-- \Thumbnail

Can someone give me an idea as to how and where to start? I usually do it in Photoshop but now that the images have grown a lot, i wanted to automate it to lower the task and time in resizing the files.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

have a look at the function getimagesize(), and the GD library for creating thumbnails, using imagecreatefromjpeg(), imagecopyresampled() and imagecreatetruecolor()

specifying which directory the image saves in is just as simple as copying to the desired directory, or saving the freshly created image in the specified directory.. (assuming you have correct permissions on the folders/files)

What I would do if I were you, is find a good thumbnail script (or write one) then use the thumbnail script to make your thumbnails, and even the larger photos... since all a thumbnail script does is resize a picture.

Some logic for you.

Open the directory using opendir().
The PHP manual provides a good example of how to loop through the contents of a directory here: http://us2.php.net/manual/en/function.opendir.php
Then, as you do your loop, create all of the various sizes one right after the other, saving them where appropriate. Then as it iterates through the loop, all of your pictures will be done in one clean sweep.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Look in our code snipplet section, we have a class that does exactly this..
neugent
Forum Newbie
Posts: 24
Joined: Wed Jul 06, 2005 3:35 am
Location: Philippines

Post by neugent »

Thanks both of you. I'll just post here if i can no longer debug my code. :)
Post Reply