I currently have a script that I have which creates thumbnails on the fly...I would use it like this:
<img src=http://www.htmlforums.com/archive/index.php/"gallerypic.php?picture=pic.jpg&width=100&height=100" />
The thing is, I'd like to make a script that creates thumbnails of all the pictures in the current dir. And I'd like to make it all one php file. Here is what I have:<?php
define('IMAGE_BASE', './');
define('MAX_WIDTH', 100);
define('MAX_HEIGHT', 100);
$image_list = array();
if(is_dir(IMAGE_BASE) && $dir = opendir(IMAGE_BASE)){
while (($file = readdir($dir)) !== false){
if (filetype(IMAGE_BASE.$file)=="file" && preg_match('/(jpg|jpeg|png|gif)$/i',$file)){
$image_list[] = $file;
}
}
}else{echo "Internal Error: IMAGE_BASE is not a valid directory.";}
for($i=0;$i<sizeof($image_list);$i++){
echo "<img src='http://www.htmlforums.com/archive/index.php/".make_tnail(IMAGE_BASE.$image_list[$i])."' />";
}
function make_tnail($image_path){
# Load image
$img = null;
if(preg_match('/(jpg|jpeg)$/i', $image_path)){
$img = @imagecreatefromjpeg($image_path);
}elseif(preg_match('/(png)$/i', $image_path)){
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
}elseif(preg_match('/(gif)$/i', $image_path)){
$img = @imagecreatefromgif($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img){
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img){
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
if($ext == 'jpg' || $ext == 'jpeg'){
header("Content-type: image/jpeg");
imagejpeg($img,'');
}elseif($ext == 'png'){
header("Content-type: image/png");
imagepng($img);
# Only if your version of GD includes GIF support
}elseif($ext == 'gif'){
header("Content-type: image/png");
imagepng($img);
}
}
?>The red part doesn't seem to work like I had thought. I don't see the pictures. I DO see little "broken image" boxes. The actual output looks like this:<img src='' /><img src='' /><img src='' /><img src='' /><img src='' /><img src='' />thats because to generate an image you have to send the image header, so the browser can interpret the image data sent. you cant just echo the image binary data into the src of an image (well, i think you can in mozilla, but not IE). in your script it doesnt actually echo anything since you arent returning anything in the function, you just sending an image header and then sending the image data in the function. but if you just returned the image data, it still wouldnt work
i dont really know why you need to have it all in one file, but youd have to set another variable in the get query string to tell it to echo image data.
so, some sort of pseudo code would be
$isimage = isset($_GET['img'])?true:false;
if ($isimage) {
//get the rest of the image data from
//the query string and generate an image
} else {
//do the normal html stuff that loops
//through and outputs the html
}
you would have url's for the image that are like:
index.php?img=1&picture=blah.jpg&width=123&height=456
and then the part of the script that outputs the html would just output the designated url's for the image src.have you tried to echo the contents of $image_list[] before it goes to the function to make sure the array is getting filled? also you are not returning anything from that function.Yes, I did a var_dump of the array, and I get this:array(6) {
[0]=>
string(15) "img546 copy.jpg"
[1]=>
string(16) "collage_left.gif"
[2]=>
string(18) "collage_shaped.gif"
[3]=>
string(8) "logo.gif"
[4]=>
string(6) "ph.gif"
[5]=>
string(9) "clear.GIF"
}Which is correct. I just dropped a few different files in there, and those images. It seems to correctly sort out the images, but I can't display them.
The reason I want it in one file, is because this is for a friend that has a bunch of pictures he wants to set up on the web, so I'd like to make it so that he can just drop this file in whatever dir he wants to display. I think that part of the problem is: how do I return the headers AND the file?
This is a modification of one that I use for myself. Here is how it looks:# gallerypic.php
if (!$_GET['width']){ define('MAX_WIDTH', 9999);}
else { define('MAX_WIDTH', $_GET['width']);}
if (!$_GET['height']){define('MAX_HEIGHT', 9999);}
else { define('MAX_HEIGHT', $_GET['height']);}
# Constants
define('IMAGE_BASE', '../');
# Get image location
$image_file = str_replace('..', '', $_GET['picture']);
$image_path = IMAGE_BASE . "$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefromgif($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
if ($ext == 'jpg' || $ext == 'jpeg') {
header("Content-type: image/jpeg");
imagejpeg($img);
} else if ($ext == 'png') {
header("Content-type: image/png");
imagepng($img);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
header("Content-type: image/png");
imagepng($img);
}
# Display the image
Then it is called like this:echo "<img src=http://www.htmlforums.com/archive/index.php/\"php/gallerypic.php?picture=$picture&width=$thumbnail_size&height=$thumbnail_size\" alt=\"$desc\" border=\"0\" style=\"cursorointer;\" onclick=\"openWin($width,$height,'$picture');\" />";
As you can see, the gallerypic.php file doesn't really return anything, but still passes both the header AND the thumbnail along. It seems like if you could have a .php file do this, you could make a php FUNCTION do it too.
EDIT: I now have it return the img (return imagejpeg($img); || imagepng($img); but it still doesn't work).i already explained this
you cant return image data and put that as the html src of an image. it just wont work. you cant do it. i told you how to do it in my post up above, thats the only way it can be done in one file. it works exactly like would if it were two files, except there is variable in the query string that acts as a flag to generate an image. the way you are trying to do it is impossible
<img src=http://www.htmlforums.com/archive/index.php/"gallerypic.php?picture=pic.jpg&width=100&height=100" />
The thing is, I'd like to make a script that creates thumbnails of all the pictures in the current dir. And I'd like to make it all one php file. Here is what I have:<?php
define('IMAGE_BASE', './');
define('MAX_WIDTH', 100);
define('MAX_HEIGHT', 100);
$image_list = array();
if(is_dir(IMAGE_BASE) && $dir = opendir(IMAGE_BASE)){
while (($file = readdir($dir)) !== false){
if (filetype(IMAGE_BASE.$file)=="file" && preg_match('/(jpg|jpeg|png|gif)$/i',$file)){
$image_list[] = $file;
}
}
}else{echo "Internal Error: IMAGE_BASE is not a valid directory.";}
for($i=0;$i<sizeof($image_list);$i++){
echo "<img src='http://www.htmlforums.com/archive/index.php/".make_tnail(IMAGE_BASE.$image_list[$i])."' />";
}
function make_tnail($image_path){
# Load image
$img = null;
if(preg_match('/(jpg|jpeg)$/i', $image_path)){
$img = @imagecreatefromjpeg($image_path);
}elseif(preg_match('/(png)$/i', $image_path)){
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
}elseif(preg_match('/(gif)$/i', $image_path)){
$img = @imagecreatefromgif($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img){
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img){
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
if($ext == 'jpg' || $ext == 'jpeg'){
header("Content-type: image/jpeg");
imagejpeg($img,'');
}elseif($ext == 'png'){
header("Content-type: image/png");
imagepng($img);
# Only if your version of GD includes GIF support
}elseif($ext == 'gif'){
header("Content-type: image/png");
imagepng($img);
}
}
?>The red part doesn't seem to work like I had thought. I don't see the pictures. I DO see little "broken image" boxes. The actual output looks like this:<img src='' /><img src='' /><img src='' /><img src='' /><img src='' /><img src='' />thats because to generate an image you have to send the image header, so the browser can interpret the image data sent. you cant just echo the image binary data into the src of an image (well, i think you can in mozilla, but not IE). in your script it doesnt actually echo anything since you arent returning anything in the function, you just sending an image header and then sending the image data in the function. but if you just returned the image data, it still wouldnt work
i dont really know why you need to have it all in one file, but youd have to set another variable in the get query string to tell it to echo image data.
so, some sort of pseudo code would be
$isimage = isset($_GET['img'])?true:false;
if ($isimage) {
//get the rest of the image data from
//the query string and generate an image
} else {
//do the normal html stuff that loops
//through and outputs the html
}
you would have url's for the image that are like:
index.php?img=1&picture=blah.jpg&width=123&height=456
and then the part of the script that outputs the html would just output the designated url's for the image src.have you tried to echo the contents of $image_list[] before it goes to the function to make sure the array is getting filled? also you are not returning anything from that function.Yes, I did a var_dump of the array, and I get this:array(6) {
[0]=>
string(15) "img546 copy.jpg"
[1]=>
string(16) "collage_left.gif"
[2]=>
string(18) "collage_shaped.gif"
[3]=>
string(8) "logo.gif"
[4]=>
string(6) "ph.gif"
[5]=>
string(9) "clear.GIF"
}Which is correct. I just dropped a few different files in there, and those images. It seems to correctly sort out the images, but I can't display them.
The reason I want it in one file, is because this is for a friend that has a bunch of pictures he wants to set up on the web, so I'd like to make it so that he can just drop this file in whatever dir he wants to display. I think that part of the problem is: how do I return the headers AND the file?
This is a modification of one that I use for myself. Here is how it looks:# gallerypic.php
if (!$_GET['width']){ define('MAX_WIDTH', 9999);}
else { define('MAX_WIDTH', $_GET['width']);}
if (!$_GET['height']){define('MAX_HEIGHT', 9999);}
else { define('MAX_HEIGHT', $_GET['height']);}
# Constants
define('IMAGE_BASE', '../');
# Get image location
$image_file = str_replace('..', '', $_GET['picture']);
$image_path = IMAGE_BASE . "$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefromgif($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
if ($ext == 'jpg' || $ext == 'jpeg') {
header("Content-type: image/jpeg");
imagejpeg($img);
} else if ($ext == 'png') {
header("Content-type: image/png");
imagepng($img);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
header("Content-type: image/png");
imagepng($img);
}
# Display the image
Then it is called like this:echo "<img src=http://www.htmlforums.com/archive/index.php/\"php/gallerypic.php?picture=$picture&width=$thumbnail_size&height=$thumbnail_size\" alt=\"$desc\" border=\"0\" style=\"cursorointer;\" onclick=\"openWin($width,$height,'$picture');\" />";
As you can see, the gallerypic.php file doesn't really return anything, but still passes both the header AND the thumbnail along. It seems like if you could have a .php file do this, you could make a php FUNCTION do it too.
EDIT: I now have it return the img (return imagejpeg($img); || imagepng($img); but it still doesn't work).i already explained this
you cant return image data and put that as the html src of an image. it just wont work. you cant do it. i told you how to do it in my post up above, thats the only way it can be done in one file. it works exactly like would if it were two files, except there is variable in the query string that acts as a flag to generate an image. the way you are trying to do it is impossible