Fanart Gallery - thumbnailing code

Discuss anything pertinent to this website.

Moderator:Æron

Richard K Niner
Posts:4297
Joined:Wed Oct 20, 2004 5:08 pm
Location:On hiatus
Contact:
Fanart Gallery - thumbnailing code

Postby Richard K Niner » Wed Mar 22, 2006 12:46 am

Because I was asked about it:

Code: Select all

<?php // Get the filename, remove '/'s $fname = explode('/', $_GET['image']); $fname = $fname[0]; // Okay, I have separate galleries on my website. The whole purpose of the $gallery variable is to find the two paths further down $gallery = explode('/', $_GET['gallery']); $gallery = $gallery[0]; $path = './gallery/'.$gallery.'/images/'; // Location of original images $localpath = './gallery/'.$gallery.'/thumbs/'; // Location of thumbnail images // Specifying the thumbnail size $new_height = @intval($_GET['height']); // Thumbnail height $new_width = @intval($_GET['width']); // Thumbnail width // Next two lines aren't necessary if @intval calls above are replpaced with static numbers if ($new_height == 0) $new_height = 150; if ($new_width == 0) $new_width = 200; $basename = $fname.'__'.$new_height.'_'.$new_width.'.png'; // Just making up a name for the thumbnail, here // If the file already exists, we don't need to make a new thumbnail if (!file_exists($localpath.$basename)) { // Create a GD resource of the original image (necessary for manipulation) if (strpos($fname, '.png') !== FALSE) { $im = @imagecreatefrompng($path.$fname); } elseif (strpos($fname, '.gif') !== FALSE) { $im = @imagecreatefromgif($path.$fname); } elseif (strpos($fname, '.jpg') !== FALSE || strpos($fname, '.jpeg') !== FALSE) { $im = @imagecreatefromjpeg($path.$fname); } else { header('HTTP/1.1 500 Internal Server Error'); echo('The selected image is not of a recognizable file type'); exit; } // If we enter this if statement, something wrong occured if (!$im) { header('HTTP/1.1 500 Internal Server Error'); echo('Error loading selected image'); exit; } // Now to get the original dimensions $width = imagesx($im); $height = imagesy($im); $aspectratio = ($width * 1.0) / $height; // The thumbnail is now being initialized $img = imagecreatetruecolor($new_width, $new_height); imageantialias($img, TRUE); imagealphablending($img, FALSE); imagesavealpha($img, TRUE); $transparent = imagecolorallocatealpha($img, 0, 0, 0, 255); for ($i = 0; $i < $new_width; $i++) { for ($j = 0; $j < $new_height; $j++) { imagesetpixel($img, $i, $j, $transparent); } } // Now to copy the image over. Don't try to understand this, it causes some headaches if ($aspectratio < (($new_width * 1.0) / $new_height)) { if (imagecopyresampled($img, $im, intval(($new_width - $new_height * $aspectratio) / 2), 0, 0, 0, intval($new_height * $aspectratio), $new_height, $width, $height) !== TRUE) { header('HTTP/1.1 500 Internal Server Error'); echo('An error has occured during image resizing'); exit; } } elseif ($aspectratio > (($new_width * 1.0) / $new_height)) { if (imagecopyresampled($img, $im, 0, intval(($new_height - $new_width / $aspectratio) / 2), 0, 0, $new_width, intval($new_width / $aspectratio), $width, $height) !== TRUE) { header('HTTP/1.1 500 Internal Server Error'); echo('An error has occured during image resizing'); exit; } } else { if (imagecopyresampled($img, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height) !== TRUE) { header('HTTP/1.1 500 Internal Server Error'); echo('An error has occured during image resizing'); exit; } } // Now, to save the result imagepng($img, $localpath.$basename); imagedestroy($img); } // Outputs the thumbnail header('Content-Type: image/png'); $fp = fopen($localpath.$basename, 'r'); while (!feof($fp)) { echo fread($fp, 8192); } fclose($fp); ?>
<center>Image
K9U | Dog House | Av rotation</center>

Return to “The Site”

Who is online

Users browsing this forum: No registered users and 9 guests