PHP Thumbnail Generator Not Working

Status
Not open for further replies.

majestyc

New Member
Messages
26
Reaction score
1
Points
1
Hello, I had worked on a code to download an image to the server, resize it and then display the new image. I have it working on my local server but once I try to use it here on x10host, it just continues running or just shows blank. I was wondering if there is a part of my code that is not allowable within the free hosting if you could look it over and let me know I would appreciate it and see if there is another method that I can use here :)

Here is my class code which is where I think the issue is, but I don't know for sure.

class thumbnailGenerator {


var $allowableTypes = array(
IMAGETYPE_GIF,
IMAGETYPE_JPEG,
IMAGETYPE_PNG
);

public function imageCreateFromFile($filename, $imageType) {

switch($imageType) {
case IMAGETYPE_GIF : return imagecreatefromgif($filename);
case IMAGETYPE_JPEG : return imagecreatefromjpeg($filename);
case IMAGETYPE_PNG : return imagecreatefrompng($filename);
default : return false;
}

}

/**
* Generates a thumbnail image using the file at $sourceFilename and either writing it
* out to a new file or directly to the browser.
*
* @param string $sourceFilename Filename for the image to have thumbnail made from
* @param integer $maxWidth The maxium width for the resulting thumbnail
* @param integer $maxHeight The maxium height for the resulting thumbnail
* @param string $targetFormatOrFilename Either a filename extension (gif|jpg|png) or the
* filename the resulting file should be written to. This is optional and if not specified
* will send a jpg to the browser.
* @return boolean true if the image could be created, false if not
*/
public function generate($sourceFilename, $maxWidth, $maxHeight, $targetFormatOrFilename = 'jpg') {

set_time_limit(0);
$size = getimagesize($sourceFilename); // 0 = width, 1 = height, 2 = type

// check to make sure source image is in allowable format
if(!in_array($size[2], $this->allowableTypes)) {
return false;
}

// work out the extension, what target filename should be and output function to call
$pathinfo = pathinfo($targetFormatOrFilename);
if($pathinfo['basename'] == $pathinfo['filename']) {
$extension = strtolower($targetFormatOrFilename);
// set target to null so writes out to browser
$targetFormatOrFilename = null;
}
else {
$extension = strtolower($pathinfo['extension']);
}

switch($extension) {
case 'gif' : $function = 'imagegif'; break;
case 'png' : $function = 'imagepng'; break;
default : $function = 'imagejpeg'; break;
}

// load the image and return false if didn't work
$source = $this->imageCreateFromFile($sourceFilename, $size[2]);
if(!$source) {
return false;
}

// write out the appropriate HTTP headers if going to browser
if($targetFormatOrFilename == null) {
if($extension == 'jpg') {
header("Content-Type: image/jpeg");
}
else {
header("Content-Type: image/$extension");
}
}

// if the source fits within the maximum then no need to resize
if($size[0] <= $maxWidth && $size[1] <= $maxHeight) {
$function($source, $targetFormatOrFilename);
}
else {

$ratioWidth = $maxWidth / $size[0];
$ratioHeight = $maxHeight / $size[1];

// use smallest ratio
if($ratioWidth < $ratioHeight) {
$newWidth = $maxWidth;
$newHeight = round($size[1] * $ratioWidth);
}
else {
$newWidth = round($size[0] * $ratioHeight);
$newHeight = $maxHeight;
}

$target = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($target, $source, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
$function($target, $targetFormatOrFilename);

}

return true;

}

}
 

caftpx10

Well-Known Member
Messages
1,534
Reaction score
114
Points
63
Check if the 'gd' PHP extension is enabled. PHP extension management section can be found on the 'Select PHP Version' page in cPanel.
 

majestyc

New Member
Messages
26
Reaction score
1
Points
1
Check if the 'gd' PHP extension is enabled. PHP extension management section can be found on the 'Select PHP Version' page in cPanel.

Thank you for getting back with me on this. I got the issue fixed apparently it was a stupid mistake on my part and simply forgot to do a php include for my functions file lol. Once I included it into my header then it worked perfectly. I think this code might help out a lot of people though so I will leave it up for others. The use of this code is simply to do this coding:

<?php
//You don't really need this, but I put it here to let you know another variable in the coding, this variable is to be the original image URL or location, the $imageURL variable can be set from your database, an external image location, or even your own folder in your server. I use the variable since I can dynamically take product images and resize them on the fly without having to keep changing the URL.
$imageURL = http://www.google.com/LARGE IMAGE I WANT TO RESIZE.jpg;

//Change the path to meet your image locations
$pathtoImages = "img/products";

//Change the variable to match what you want your name to be. I get the image name from my database, in this case I want my images to match the ID of the sql database for example "582" would end up being "img/products/582.jpg"
$imagename = $id

//Leave this extension as .jpg
$imageExtension = ".jpg";

//Join variables together to create the entire image location
$image = $pathtoImages . $id;
$image = $image . $imageExtension;

if (file_exists($image)) {
//Do nothing since you already have the image saved as a thumbail
}
else
{
//Run the thumbnailGenerator Class and save the image as a 660x660, you can change the size to whatever you would like.
$tg = new thumbnailGenerator;
$imageURL = $tg->generate( $imageURL, 660, 660, $image);

//Not required unless you need to update your database for the image URL, just remember you have to connect to your database first before using this code
$sql = "UPDATE `products` SET `imageURL` = '$image' WHERE `products`.`id` = $id";
$result = mysqli_query($connect, $sql) or die(mysql_error());
}

echo 'img src="' . $image . '" alt="My Image Title"';
?>
 
Status
Not open for further replies.
Top