Creating thumbnails dynamically from images with PHP
Managing large gallerias or photo albums in dynamic web services needs thumbnails to be created on fly. In PHP there are built in functions which can create thumbnails at run time with given width but for more requirements like creating thumbnails with custom provided values for top, left and more supported image formats like png, gif we can use phpThumb.
First we will create a simple function to create thumbnail from image with PHP.
First we will create a simple function to create thumbnail from image with PHP.
function createThumbnail($src, $destination, $width) { $image = imagecreatefromjpeg($src); $imgW = imagesx($image); $imgH = imagesy($image); $height = floor($imgH * ($width / $imgW)); $thumb = imagecreatetruecolor($width, $height); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $imgW, $imgH); imagejpeg($thumb, $destination); }This function will create thumbnail with provided width. Now we will write function with more requests to generate thumbnail. To use this function we will need phpThumb library you can download this library from here
include("phpThumb/phpthumb.class.php"); function createThumbnail($imagePath,$filename,$destPath,$width,$height,$left,$top,$zc=1) { $imgName = explode('.', $filename); $EXT = $imgName[1]; $capture_raw_data = false; $phpThumb = new phpThumb(); $phpThumb->setSourceFilename($imagePath.$filename); $phpThumb->setParameter('sw', $width); $phpThumb->setParameter('sh', $height); $phpThumb->setParameter('sx', $left); $phpThumb->setParameter('sy', $top); $phpThumb->setParameter('zc', $zc); if(!is_dir($destPath)) { @mkdir($destPath, 0777); } $phpThumb->setParameter('config_output_format', $EXT); $phpThumb->setParameter('config_imagemagick_path', '/usr/local/bin/convert'); if ($phpThumb->GenerateThumbnail()) { if ($phpThumb->RenderToFile($destPath.$filename)) { } else { } } else { } }Share your views about how you find this post useful.
Tags:
Thumbnails

Abida
Abida is web developer works with HTML5, Javascript, PHP. She has developed many websites and now shares her knowledge and tips with Knowledge Aspire.
Get latest updates in your inbox
Popular Posts
How Yoga Nurtures Your Programming Skills
Understanding and analyzing basics of SEO
Creating SEO friendly URLs with PHP and SQL
Adobe AIR - Developing cross platform applications
Writing functions to insert, update, search, delete from SQL tables in PHP
Getting started with C#
Creating XML sitemap with PHP and SQL and submitting to search engines
Creating RSS feed in xml format with PHP and SQL
Creating thumbnails dynamically from images with PHP
Backup SQL database with PHP
Tags
Android
Java
Nashorn
CSV
Validation
PHP Mailer
cPanel
Facebook
Marketing
AdSense
Google
Technorati
Feathers
Starling
crop
PDF
Yoga
feedburner
rss feed
meta tags
SQL
backup database
Adobe Air
XML Sitemap
Syntaxhighlighter
Uploadify
Tinymce
screenshot
Text image
webmaster tools
Verifying website
web analysis
SEO
visual studio 2013
IDE