Create thumbnail image by php

9

Posted by admin | Posted in General | Posted on 03-04-2013

Tags: ,

When we upload large size images on server. Uploaded large images take more time to load on webpage, so we need to show small size images on our webpage. Image thumbnail is a solution to generate uploaded image’s thumbnail to show required size images on our website.

Resize Image, Thumbnail image

 

VN:F [1.9.22_1171]
Rating: 4.4/5 (5 votes cast)
VN:F [1.9.22_1171]
Rating: +4 (from 4 votes)
Create thumbnail image by php, 4.4 out of 5 based on 5 ratings

Comments (9)

Is there any way to create the thumbnails from images in a directory automatically?

VA:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.22_1171]
Rating: 0 (from 0 votes)
admin

Yes, First of all we will read image’s source directory by using readdir function in php.
This function will create all images as thumbnail from source directory to destination directory.
Your solution is a follows:
< ?php
$source_path = 'images/sourceimages';
$destination_path = 'images/destinationimages';

if ($handle = opendir($source_path)) {
while (false !== ($filename = readdir($handle))) {
if($filename!='.' and $filename!='..') {
createthumb($source_path.'/'.$filename, $destination_path.'/'.$filename, 125, 100);
}
}
closedir($handle);
}
?>

VN:F [1.9.22_1171]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +3 (from 3 votes)

Can you kindly expain the directory path where is the script to be uploaded, where is the images folder and its relation to the directory path that the script is uploaded to.

Also, does the script automatically creates the folder “destinationimages”? and “sourceimages” is the folder where the images are, isn’t it?

VA:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.22_1171]
Rating: +2 (from 2 votes)
admin

Hiii..
Here we are assuming that we stand at root path of website and have images named folder and it’s sub-directories sourceimages and destinationimages. Sourceimages directory contains all images what we want to convert to thumbnail size and destinationimages directory is used to save new created/generated thumbnail images by this script.

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)

Works great! Thanks.

VA:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.22_1171]
Rating: +1 (from 1 vote)

I tried to use this code, but it’s able to resize only ten pictures and then throws a error ‘unable to load image source’. I changed:
ini_set(‘memory_limit’,’512M’);
to 1024M, thinking in a memory problem, but nothing changes. Can you have any idea about this problem?

VA:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.22_1171]
Rating: 0 (from 0 votes)
admin

Yes, If we are fetching an image folder to create thumbnail images. We will have to skip following three files of that image folder:
A . (Dot extension file)
B .. (Double dot extension file)
C thumb.db (It is created automatically when we see images in thumbnail view in explorer)
Both files are not type of images that’s why function is not able to read that type of files and showing such error message. We can set memory_limit, max_execution_time and set_time_limit options, if images are large in size.

VN:F [1.9.22_1171]
Rating: 5.0/5 (3 votes cast)
VN:F [1.9.22_1171]
Rating: +3 (from 3 votes)

Muchas Gracias!

VA:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.22_1171]
Rating: +3 (from 3 votes)

Works on WordPress?? Thanks!

VA:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Write a comment

*