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.phpCode: 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>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]