Dynamically creating a name for output

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
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Dynamically creating a name for output

Post 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?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

tip: concatenate the string name:

'oldname'.'_t'
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post 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.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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);
?>
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post by Zamees »

ok cool, lemme try that.
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post 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&#1111;"f"])) &#123;
    notfound();
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post 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.
Last edited by Zamees on Mon Oct 06, 2003 12:36 pm, edited 1 time in total.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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);
?>
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post 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?
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post 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.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Kinda lost me there. I think you will have to post some code to help clear it up.
Zamees
Forum Newbie
Posts: 19
Joined: Mon Oct 06, 2003 11:27 am

Post 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("/\+*((&#1111;a-z])\((&#1111;^\)]+)\))\+*/", $QUERY_STRING,
                $matches, PREG_SET_ORDER);

// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "&#1111;^)+]+",
                "w" => "&#1111;0-9]+%?",
                "h" => "&#1111;0-9]+%?",
                "x" => "&#1111;0-9]+",
                "y" => "&#1111;0-9]+",
                "t" => "jpg|png|JPG",
                "q" => "1?&#1111;0-9]&#123;1,2&#125;" );


// check tags and save correct values in array
for ($i=0; $i<count($matches); $i++) &#123;
    if (isset($check&#1111;$matches&#1111;$i]&#1111;2]])) &#123;
        if (preg_match('/^('.$check&#1111;$matches&#1111;$i]&#1111;2]].')$/',
               $matches&#1111;$i]&#1111;3])) &#123;
            $tags&#1111;$matches&#1111;$i]&#1111;2]] = $matches&#1111;$i]&#1111;3];
        &#125;
    &#125;
&#125;

function notfound() &#123;
    header("HTTP/1.0 404 Not Found");
    exit;
&#125;

// check that filename is given
if (!isset($tags&#1111;"f"])) &#123;
    notfound();
&#125;

// check if file exists
if (!file_exists($base_img_dir.$tags&#1111;"f"])) &#123;
    notfound();
&#125;

// retrieve file info
$imginfo = getimagesize($base_img_dir.$tags&#1111;"f"]);

// load image
switch ($imginfo&#1111;2]) &#123; 
    case 2:     // jpg
        $img_in = imagecreatefromjpeg($base_img_dir.$tags&#1111;"f"]) or notfound();
        if (!isset($tags&#1111;"t"])) &#123;
            $tags&#1111;"t"] = "jpg";
        &#125;
        break;
    case 3:     // png
        $img_in = imagecreatefrompng($base_img_dir.$tags&#1111;"f"]) or notfound();
        if (!isset($tags&#1111;"t"])) &#123;
            $tags&#1111;"t"] = "png";
        &#125;
        break;
    case 4:    // JPG
        $img_in = imagecreatefromjpeg($base_img_dir.$tags&#1111;"f"]) or notfound();
        if (!isset($tags&#1111;"t"])) &#123;
            $tags&#1111;"t"] = "JPG";
        &#125;
        break;
    default:
        notfound();
&#125;

// check for maximum width and height
if (isset($tags&#1111;"x"])) &#123;
    if ($tags&#1111;"x"] < imagesx($img_in)) &#123;
        $tags&#1111;"w"] = $tags&#1111;"x"];
    &#125;
&#125;
if (isset($tags&#1111;"y"])) &#123;
    if ($tags&#1111;"y"] < imagesy($img_in)) &#123;
        $tags&#1111;"h"] = $tags&#1111;"y"];
    &#125;
&#125;

// check for need to resize
if (isset($tags&#1111;"h"]) or isset($tags&#1111;"w"])) &#123;
    // convert relative to absolute
    if (isset($tags&#1111;"w"])) &#123;
        if (strstr($tags&#1111;"w"], "%")) &#123;
            $tags&#1111;"w"] = (intval(substr($tags&#1111;"w"], 0, -1)) / 100) *
                          $imginfo&#1111;0];
        &#125;
    &#125;
    if (isset($tags&#1111;"h"])) &#123;
        if (strstr($tags&#1111;"h"], "%")) &#123;
            $tags&#1111;"h"] = (intval(substr($tags&#1111;"h"], 0, -1)) / 100) *
                          $imginfo&#1111;1];
        &#125;
    &#125;

    // resize
    if (isset($tags&#1111;"w"]) and isset($tags&#1111;"h"])) &#123;
        $out_w = $tags&#1111;"w"];
        $out_h = $tags&#1111;"h"];
    &#125; elseif (isset($tags&#1111;"w"]) and !isset($tags&#1111;"h"])) &#123;
        $out_w = $tags&#1111;"w"];
        $out_h = $imginfo&#1111;1] * ($tags&#1111;"w"] / $imginfo&#1111;0]);
    &#125; elseif (!isset($tags&#1111;"w"]) and isset($tags&#1111;"h"])) &#123;
        $out_w = $imginfo&#1111;0] * ($tags&#1111;"h"] / $imginfo&#1111;1]);
        $out_h = $tags&#1111;"h"];
    &#125; else &#123;
        $out_w = $tags&#1111;"w"];
        $out_h = $tags&#1111;"h"];
    &#125;
    
    // 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));
&#125; else &#123;
    // no resize needed
    $img_out = $img_in;
&#125;

// check for a given jpeg-quality, otherwise set to default
if (!isset($tags&#1111;"q"])) &#123;
    $tags&#1111;"q"] = 75;
&#125;


$new_image = $base_img_dir."t_".$tags&#1111;"f"];

// returning the image
switch ($tags&#1111;"t"]) &#123;
    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();
&#125;

?>
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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 :) )
Post Reply