Jump to content

Thumbnail Making Script


cosmic_sniper

Recommended Posts

I would be needing different picture sizes on different pages on my site but saving different sizes of a single pic is space consuming.

 

I've heard that it's possible to automatically adjust the size of the picture and found a script that ought to make that possible. Unfortunately, it did not work for me.

 

Article: PHP Thumbnail Script Using GD http://www.rainbodesign.com/pub/thumbnails/

 

Here's the code:

<?
// Rainbo Design PHP Thumbnail Maker
// Copyright (C) 2005-2010 by Richard L. Trethewey - rick@rainbo.net
// All Rights Reserved
// If you use this script, I'd appreciate a link!
// http://www.rainbodesign.com/pub/

// Defaults
$thumbsize = 150;	 // Default thumbnail width.
$imagesource = 'images/default_img.jpg';	// Default image file name.
// Set to empty string for no image output on failure.
$error = '';

if (isset($_GET['width'])) { $thumbsize = $_GET['width']; }
if (isset($_GET['src'])) { $imagesource = $_GET['src']; }

$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);

if (file_exists($imagesource)) {

if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); 
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); 
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);

$imagewidth = imagesx($image);
$imageheight = imagesy($image); 

if ($imagewidth >= $thumbsize) {
$thumbwidth = $thumbsize;
$factor = $thumbsize / $imagewidth;
$thumbheight = floor($imageheight * $factor);
} else {
$thumbwidth = $imagewidth;
$thumbheight = $imageheight;
$factor = 1;
}

// Create a thumbnail-sized GD Image object
$thumb = @imagecreatetruecolor($thumbwidth,$thumbheight);

// bool imagecopyresized ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
imagecopyresized($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);

// Send output to user as a jpeg type, regardless of original type
header("Content-type:image/jpeg;");
imagejpeg($thumb);
imagedestroy($image);
imagedestroy($thumb);
} else {
$error = "File $imagesource Not Found";
} // endif file_exists

if ($error != '') {
header('Content-type:text/plain;');
echo($error);
exit;
} // endif $error

?>

 

And here's a little instruction (which I have followed as well).

All it takes is replacing the "src" parameter in the <img> tag with the URL for the PHP script, including the parameters "src" which contains the path to the original image file, and "color" to select the color for the font. The script also supports a "width" parameter to specify the width of your thumbnail image. Using that script to display the above image as a thumbnail by setting the "src" attribute to "makeThumb.php?src=slides/slide1.jpg", we get the resulting image shown on the right.

 

Can anyone please help me with this script or something similar?

 

Thanks in advance. ^_^

 

Edit: The problem is that the image is not showing. It appears as if it cannot locate the file source.

Link to comment
Share on other sites

In most cases, disk space is less valuable than CPU time. Is there any reason in particular you need to save disk space? Thumbnails shouldn't take up more than ~30KB each. That's nearly 2,000 thumbnails that can fit into a 50MB space.

I'd suggest resizing the images once and saving, rather than doing it on the fly.

 

Keep in mind, the script you've posted allows a user to output any image that PHP has access to, including ones outside of your web-root.

 

script.php?src=../../../premium/some_premium_image.jpg

Link to comment
Share on other sites

Well, the reason why I intend to do it using a script is for it to be more convenient and easier to maintain. The website I'm working on has quite a number of images on it and these images would be reflected on different pages with different size that would be needed. In addition to that these images are being updated / replaced from time to time. So, having a single image file and a script that would manage the size of the image would be great.

 

But since you pointed it out, I think having a PHP script with such access (access beyond web-root) is the reason why it's not working on the server. I'm using a shared hosting service which prevents such access (necessarily).

 

Is there any other script that might be able to achieve the goal that I wanted or do I really have to resort to saving multiple files?

Link to comment
Share on other sites

I don't mean to confuse you or anything but this code works great for me. Just put in a directory with the page that includes it and in the same directory put you img and thumbs file.

 

<?php

function createThumbs(){

$imgPath = "imgs/";
$thumbPath = "thumbs/";
$thumbsWidth = 100;

$imgDir = opendir( $imgPath );

        while ( false != ($dirContents = readdir( $imgDir ))){
        	 
        	$imgInfo = pathInfo( $imgPath . $dirContents );
        	
        	/*strtolower converts a string to all lowercase; pathInfo
        	  returns an array of info on file path; in this case pathInfo['extension']
        	  returns the file extension */
        	if ( strtolower( $imgInfo['extension']) == 'jpg' )
        	{
        		$jpgImg = imagecreatefromjpeg( "{$imgPath}{$dirContents}" );
        		$imgWidth = imagesx( $jpgImg );
        		$imgHeight = imagesy( $jpgImg );
        		
        		$new_width = $thumbsWidth;
        		$thumbsHeight = floor( $imgHeight * ($new_width/$imgWidth ));
        		
        		$tmp_img = imagecreatetruecolor( $thumbsWidth, $thumbsHeight );
        		imagecopyresized( $tmp_img, $jpgImg, 0, 0, 0, 0, $new_width, $thumbsHeight, $imgWidth, $imgHeight );
        	        imagejpeg( $tmp_img, "{$thumbPath}{$dirContents}" );
        	}        	
        	
         }
        closedir( $imgDir );	
}

createThumbs();



function imgGallery(){

$imgPath = "imgs/";
$thumbsPath = "thumbs/";

$thumbDir = openDir( $thumbsPath );

        
        $filname = $imgInfo['filname'];

//while not the end of the directories
echo "<table align=\"center\">";
echo "<tr>";
while ( False !== ($thumbDirContents = readDir( $thumbDir ))){
	$imgInfo = pathInfo( $thumbsPath . $thumbDirContents );
      	
	if (strtolower( $imgInfo['extension']) == 'jpg'){	
	        echo "<td>";
		echo "<img class=\"smallie\" src=\"{$thumbsPath}{$thumbDirContents}\" />";
		echo "</td>";

		$counter += 1;
                 if ( $counter % 5 == 0 ) { echo "</tr><tr>"; }

	}
           }
           echo "</tr>";
           echo "</table>";
     				
       
           closeDir ( $thumbDir );
           
           
}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.