PHP Thumbnail Creation Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
megamoose
Forum Newbie
Posts: 5
Joined: Fri Nov 24, 2006 3:21 am

PHP Thumbnail Creation Problem

Post 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]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP Thumbnail Creation Problem

Post by onion2k »

megamoose wrote:$image = imagecreatefromjpeg($originial);
The error is on this line. I'll let you figure it out.
User avatar
megamoose
Forum Newbie
Posts: 5
Joined: Fri Nov 24, 2006 3:21 am

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

No, have a very close look at exactly what's in the line I pointed out.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

A typo in the variable name.
User avatar
megamoose
Forum Newbie
Posts: 5
Joined: Fri Nov 24, 2006 3:21 am

Post by megamoose »

Brilliant. What a silly mistake that was. It now works perfectly. Thanks a bunch guys ;)

Mark
Post Reply