Page 1 of 1
Dynamically creating a name for output
Posted: Mon Oct 06, 2003 11:27 am
by Zamees
Im attempting to resize an image and output it to a folder. The code works great for resizing, and I can get it output if I hard code the name, but how do I create the same name as the filename that was passed in and add a _t to it
the input is the f(x)
Code: Select all
// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png|JPG) type of output
// q(100) quality of jpeg
the output line looks like such
Code: Select all
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, "");
exit;
In that output, it steams to the browser, so i'd like to replace the "" with the filename coming in with a _t
so the line would look like this
imagejpeg($img_out,"filename_t");
What do I stick in there instead of filename_t?
Posted: Mon Oct 06, 2003 11:32 am
by AVATAr
tip: concatenate the string name:
'oldname'.'_t'
Posted: Mon Oct 06, 2003 12:02 pm
by Zamees
Thanx, but I know next to nothing about php. If this were ASP, its simple as pie, but since its PHP, im not too sure how to use the
filename in the $img_out section What does the code look like for that, an example would greatly help. When i hard code in a name, it works fine, but having it pull the info that was passed in is what tickles my noodle.
Posted: Mon Oct 06, 2003 12:12 pm
by Stoneguard
One confusing issue you have here is that the $img_out is actually the image coming in, or at least that may be confusing you.
An equivilent ASP code might be:
Code: Select all
Dim img_out
Dim new_file
' Not sure how you get the actual file name
new_file = img_out & "_t"
imagejpeg img_out, new_file
In php, you would write:
Code: Select all
<?php
$new_file = $img_out . "_t";
imagejpeg($img_out, $new_file);
?>
Posted: Mon Oct 06, 2003 12:14 pm
by Zamees
ok cool, lemme try that.
Posted: Mon Oct 06, 2003 12:19 pm
by Zamees
OK, i looked at it again, the thing is, I have an image passed in, it is resized and then the dimensions are stored into $img_out, but the name of the file wouldn't be in $img_out, right? It appears the name of the file is inside the f(x)
That is how I call this script
Code: Select all
<img src=img.php?f(somepicture.jpg)+h(50)>
and it resizes the image to a 50 height. Im attempting to cache this 50 height image, so i'll need to know the filename when passed in. How can I go about retrieving that out of the f(x)
Heres a piece that checks to see if hte file is a valid filename
Code: Select all
// check that filename is given
if (!isset($tagsї"f"])) {
notfound();
Posted: Mon Oct 06, 2003 12:30 pm
by Zamees
Getting closer, i changed it around a bit, and i got the image to store as
filename.jpg_t
I used this
$new_image = $base_img_dir.$tags["f"]."_t";
So what way would i change that around to get the _t on the inside of the .jpg
Nevermind, DUH... I just changed it to
$base_img_dir."t_".$tags and so forth, works great, stores the image, should help with my server issue immesly. thanks for you help man.
Posted: Mon Oct 06, 2003 12:33 pm
by Stoneguard
then you should be able to get the filename from the tags array variable:
Code: Select all
<?php
$cur_file = $tags["f"];
$new_file = $cur_file . "_t";
imagejpeg($img_out, $new_file);
?>
Out of curiosity, wher is $img_out being set? it still looks to be the name of the jpeg file, since that is the first parameter on the imagejpeg function.
Posted: Mon Oct 06, 2003 12:36 pm
by Stoneguard
a simple replace would do that, or using a function to break apart the file name. I would just call:
Code: Select all
<?php
$new_image = $base_img_dir.$tags["f"];
$new_file = str_replace( ".jpg", "_t.jpg", $new_image);
?>
Posted: Mon Oct 06, 2003 1:23 pm
by Zamees
Well, all i posted was the returning function. The rest of the code takes the image resizes it and then stores the variables to the $img_out.
Im almost home free now. I got it all working great. I did a fileexist, and if not, it calls the script, generates the picture stores it in the folder and then displays the picture...but...
the only way i can think to call this script is the basic formula i was given..which is
<img src="img.php?f(filename.jpg)+h(50)"> The problem with that is that now that the script has the imagejpeg($img_out, $new_image); It seems to want to store the file in the folder and then output a red x to the screen. Any fixes for this?
Posted: Mon Oct 06, 2003 1:33 pm
by Stoneguard
I think all you need do is call the imagejpeg function again without the second parameter. If you dont specify a file name the image is streamed out, but if you specify a file, it is no longer streamed out.
Posted: Mon Oct 06, 2003 1:47 pm
by Zamees
Thats the thing, I only wanted to call it once, just so it would store the image to file, and then i just call it from there. So when I do the <img src= and have the return as $img_out, $new_file, it saves the file to the folder, AND it outputs the red x to the screen. THen i immediately call the file from the folder and it outputs the picture correctly next to the error image box.
Posted: Mon Oct 06, 2003 2:01 pm
by Stoneguard
Kinda lost me there. I think you will have to post some code to help clear it up.
Posted: Mon Oct 06, 2003 2:17 pm
by Zamees
ok code coming, my solution as of now, is call the script from the img src and then set the height and border to 0

then when i call it from the folder it loads nicely.
The resize image script
As stated above called to like so... <img src=img.php?f(filename.jpg)>
Code: Select all
<?php
// define the base image dir
$base_img_dir = "./";
// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png|JPG) type of output
// q(100) quality of jpeg
// find tags
preg_match_all("/\+*((їa-z])\((ї^\)]+)\))\+*/", $QUERY_STRING,
$matches, PREG_SET_ORDER);
// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "ї^)+]+",
"w" => "ї0-9]+%?",
"h" => "ї0-9]+%?",
"x" => "ї0-9]+",
"y" => "ї0-9]+",
"t" => "jpg|png|JPG",
"q" => "1?ї0-9]{1,2}" );
// check tags and save correct values in array
for ($i=0; $i<count($matches); $i++) {
if (isset($checkї$matchesї$i]ї2]])) {
if (preg_match('/^('.$checkї$matchesї$i]ї2]].')$/',
$matchesї$i]ї3])) {
$tagsї$matchesї$i]ї2]] = $matchesї$i]ї3];
}
}
}
function notfound() {
header("HTTP/1.0 404 Not Found");
exit;
}
// check that filename is given
if (!isset($tagsї"f"])) {
notfound();
}
// check if file exists
if (!file_exists($base_img_dir.$tagsї"f"])) {
notfound();
}
// retrieve file info
$imginfo = getimagesize($base_img_dir.$tagsї"f"]);
// load image
switch ($imginfoї2]) {
case 2: // jpg
$img_in = imagecreatefromjpeg($base_img_dir.$tagsї"f"]) or notfound();
if (!isset($tagsї"t"])) {
$tagsї"t"] = "jpg";
}
break;
case 3: // png
$img_in = imagecreatefrompng($base_img_dir.$tagsї"f"]) or notfound();
if (!isset($tagsї"t"])) {
$tagsї"t"] = "png";
}
break;
case 4: // JPG
$img_in = imagecreatefromjpeg($base_img_dir.$tagsї"f"]) or notfound();
if (!isset($tagsї"t"])) {
$tagsї"t"] = "JPG";
}
break;
default:
notfound();
}
// check for maximum width and height
if (isset($tagsї"x"])) {
if ($tagsї"x"] < imagesx($img_in)) {
$tagsї"w"] = $tagsї"x"];
}
}
if (isset($tagsї"y"])) {
if ($tagsї"y"] < imagesy($img_in)) {
$tagsї"h"] = $tagsї"y"];
}
}
// check for need to resize
if (isset($tagsї"h"]) or isset($tagsї"w"])) {
// convert relative to absolute
if (isset($tagsї"w"])) {
if (strstr($tagsї"w"], "%")) {
$tagsї"w"] = (intval(substr($tagsї"w"], 0, -1)) / 100) *
$imginfoї0];
}
}
if (isset($tagsї"h"])) {
if (strstr($tagsї"h"], "%")) {
$tagsї"h"] = (intval(substr($tagsї"h"], 0, -1)) / 100) *
$imginfoї1];
}
}
// resize
if (isset($tagsї"w"]) and isset($tagsї"h"])) {
$out_w = $tagsї"w"];
$out_h = $tagsї"h"];
} elseif (isset($tagsї"w"]) and !isset($tagsї"h"])) {
$out_w = $tagsї"w"];
$out_h = $imginfoї1] * ($tagsї"w"] / $imginfoї0]);
} elseif (!isset($tagsї"w"]) and isset($tagsї"h"])) {
$out_w = $imginfoї0] * ($tagsї"h"] / $imginfoї1]);
$out_h = $tagsї"h"];
} else {
$out_w = $tagsї"w"];
$out_h = $tagsї"h"];
}
// new image in $img_out
$img_out = imagecreatetruecolor($out_w, $out_h);
imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out),
imagesy($img_out), imagesx($img_in), imagesy($img_in));
} else {
// no resize needed
$img_out = $img_in;
}
// check for a given jpeg-quality, otherwise set to default
if (!isset($tagsї"q"])) {
$tagsї"q"] = 75;
}
$new_image = $base_img_dir."t_".$tagsї"f"];
// returning the image
switch ($tagsї"t"]) {
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, $new_image);
imagejpeg($img_out, "");
exit;
case "png":
header("Content-type: image/png");
imagepng($img_out, "");
exit;
case "JPG":
header("Content-type: image/jpeg");
imagejpeg($img_out, $new_iamge);
exit;
default:
notfound();
}
?>
Posted: Mon Oct 06, 2003 2:23 pm
by Stoneguard
Ok, I think you can also replace:
Code: Select all
<?php
header("Content-type: image/jpeg");
imagejpeg($img_out, $new_image);
imagejpeg($img_out, "");
exit;
?>
with
Code: Select all
<?php
header("Content-type: image/jpeg");
imagejpeg($img_out, $new_image);
readfile($new_image);
exit;
?>
This will basically stream the new image from disk to the output for you. I have not tested it, but it should work (I hope, I am still new to PHP

)