Page 1 of 1

PHP Thumbnail Creation Problem

Posted: Fri Nov 24, 2006 3:25 am
by megamoose
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys. I want a script that will automatically create a thumbnail from an uploaded images. It's for my phpbb3 styles website and basically someone will upload a preview of their style which will be pretty big and then there will be a thumbnail that they click to enlarge it.
 
The majority of the script is done but I'm having a little problem. When it trys to create a thumbnail all I get is a smaller sized image full of black.
 
Here is the code I am currently using.
 
[b]This is function that will create a thumbnail based on parameters[/b]

Code: Select all

function createThumbnail ($original, $thumbnail, $width, $height, $quality) {
  list($width_orig, $height_orig) = getimagesize($original);
  if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
  }
       else {
   $height = ($width / $width_orig) * $height_orig;
  }
  $image_p = imagecreatetruecolor($width, $height);
  $image = imagecreatefromjpeg($originial);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  imagejpeg($image_p, $thumbnail, $quality);
  return;
}

This is the code that is used to create the thumbnail

Code: Select all

$filetitle = $this->mainclass->database->userinput($_FILES['previewfile']['name']);
 
              $filename = MD5(time() . $this->mainclass->phpbb->user->data["username"] . $_FILES['previewfile']['name'] . rand(5000, 15000)) . ".preview.jpg";
 
            move_uploaded_file($_FILES['previewfile']['tmp_name'], $this->mainclass->serverpath . "/uploads/screenshots/" . $filename);                  
 
$this->createThumbnail($this->mainclass->serverpath . "/uploads/screenshots/" . $filename, $this->mainclass->serverpath . "/uploads/screenshots/thumbs/" . $filename, 150, 150, 100);

A big thanks to everyone who trys to help me with this :) Thanks

Oh! and to anyone who wants to see the uploaded version that doesn't actually work, you can see it here

Mark


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: PHP Thumbnail Creation Problem

Posted: Fri Nov 24, 2006 3:38 am
by onion2k
megamoose wrote:$image = imagecreatefromjpeg($originial);
The error is on this line. I'll let you figure it out.

Posted: Fri Nov 24, 2006 4:21 am
by megamoose
Hey onion2k, thanks for your help :)

So is it because $original is a string and not an actual image. If I define it as an image should that work. I'll look on PHP.net and see if I can find some help then :)

Mark

Posted: Fri Nov 24, 2006 5:59 am
by onion2k
No, have a very close look at exactly what's in the line I pointed out.

Posted: Fri Nov 24, 2006 6:22 am
by bokehman
A typo in the variable name.

Posted: Fri Nov 24, 2006 5:54 pm
by megamoose
Brilliant. What a silly mistake that was. It now works perfectly. Thanks a bunch guys ;)

Mark