Need help with image thumbnail creation code

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
kday
Forum Newbie
Posts: 8
Joined: Tue Oct 24, 2006 5:47 pm

Need help with image thumbnail creation code

Post by kday »

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]


I am trying to get my code to generate and display thumbnails of already existing pictures in my "images" folder. I am new to php, and I realize I may be doing something totally wrong. If I made some errors, please give me some constructive feedback on how to solve my problems. My page does not show any php errors. It just does nothing. In fact, the browser (Safari) actually tries to download the actual php file. Firefox just prints the location of the php file in the browser. If I take the image/jpg header out, the page displays garbled up text as I would expect.

I tried the scripts on 2 different machines: My web server and local machine. They are both running PHP Version 5.1.6 with the gd library enabled. I also set the permissions to 0777 if that makes any difference.

I realize my thumbnails may look all stretched and funny, but I don't care at this point. I just want some type of image to display.

I am using 2 php files. test.php to display the images, and spanimages.php

test.php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>untitled</title>
</head>

<body>
<?php
require_once('spanimages.php');

echo span_images();
?>

</body>
</html>
spanimages.php

Code: Select all

<?php

function span_images() {
  $files = new DirectoryIterator('./images');
  $images = array();
  
  foreach($files as $im) {
    if(preg_match("/[.]jpg$/", $im))
      $images[] = "$im";
  }
  
  $picnum = count($images);
  $output = '';
  
  $new_h = 200;
  $new_w = 200;
  
  for($i=0; $i<$picnum; $i++) {
    $imagedata = getimagesize("images/$images[$i]");
    $newimage = imagecreatetruecolor($new_w, $new_h);
    $image = @imagecreatefromjpeg("images/$images[$i]");
    
    if ($image === false) {
      return 'Unable to open image';
    }
    
    $width = imagesx($image);
    $height = imagesy($image);
    imagecopyresampled ($newimage, $image, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
    
    $output .= header("Content-type: image/jpg");
    $output .= imagejpeg($newimage);
  }
  
  return $output;
}
?>

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
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

add error_reporting( E_ALL ); at the very top of your page and remove @ infront the functions

error_reporting( E_ALL ) will display any errors, warnings that your script may have
@ supreses the function from returning an error, so even if your function is crashing you'll not know
kday
Forum Newbie
Posts: 8
Joined: Tue Oct 24, 2006 5:47 pm

Post by kday »

wtf wrote:add error_reporting( E_ALL ); at the very top of your page and remove @ infront the functions

error_reporting( E_ALL ) will display any errors, warnings that your script may have
@ supreses the function from returning an error, so even if your function is crashing you'll not know
Ok, I did what you say, and it's still not reporting an error. It seems that php is just ignoring the code once it tries to create the image/jpg header. I'm sure there is a logic problem here as I don't really know what I am doing. My mistake is probably pretty noobish and easy to spot.
kday
Forum Newbie
Posts: 8
Joined: Tue Oct 24, 2006 5:47 pm

Post by kday »

I got the code working now. My logic didn't really make sense. Instead of trying to echo the image via the spanimages file (which didn't make sense at all the way I was doing it), I just use spanimages.php to create the thumbs, and printed them out via the img tag on my test.php file. Also, I think my code was actually working anyway to create the thumbs, but I wrote the thumbnails to the root directory by accident.

Ah, I hate stupid mental errors.
Post Reply