Link to home
Start Free TrialLog in
Avatar of xberry
xberryFlag for Germany

asked on

PHP filename for thumbnail in src of img tag ?

I have a PHP-script that reads image-file-source from URL-GET-parameter
and does create a thumbnail preview by using imageCopyResized() method.
I don't think the script is the problem, since i took it  from available standard expert listing,
which is supposed to work ok, but I think the problem is how to include it into html code,
so that it really works. I followed a recommendation to do it like that:

 <img src="thumbs.php?file=C++-cert-web.jpg">

but it doesn't provide the desired result (blank screen).
When I view the html source then in the browser
it appears unprocessed, so I think the recommendation
to name the php file as img src, won't really work and is a
printing mistake ? Maybe I am wrong ?
In my estimation it would only work if using php code
in the html file and retrieving the thumbnail by
using PHP code with require() or including appropriate function
returning the desired image in place of img src ?
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER

Thanks for the idea, but it doesn't seem to help,
so I decided to post the source code of the 'thumb.php'.
Please check because of following reason: my Apache here
on my computer is interpreting on base of PHP 5.0
but the book where I took the code from isn't up to date
as I am afraid. Maybe I did miss on something.

<?php
/* setting variables */
$src_file = $_GET['file'];
$max_px = 200;
$dst_w = 0;
$dst_h = 0;

        /* open depending on filetype */
        $pos = strrpos($src_file, '.') + 1;
        $file_ext = substr($file, $pos);
        switch(strToLower($file_ext)) {
                case 'png' :            $image_src = imageCreateFromPng($src_file);
                        break;
                case 'jpg' :  
                case 'jpeg':            $image_src = imageCreateFromJPEG($src_file);
                        break;
        }
                                                                   
        /* get size of image */
        if (!image_src) {
                echo "jpg wurde nicht geladen.";
        } else {
        $src_w = imagesx($image_src);
        $src_h = imagesy($image_src);
        }
       
        /* set size of thumbnail */
        if ($src_w > $src_h) {
                $dst_w = $max_px;
                $dst_h = $max_px / ($src_w / $src_h);
        } else if ($src_w < $src_h) {
                $dst_h = $max_px;
                $dst_w = $max_px / ($src_h / $src_w);
        } else {
                $dst_h = $max_px;
                $dst_w = $max_px;
        }
               
        /* copy and edit image */
        $image_dst = imageCreateTrueColor($dst_w, $dst_h);
        imageCopyResized($image_dst, $image_src, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        header('Content-Type: image/jpg');
        imagejpeg($image_dst, $src_file);
        imageDestroy($image_src);
        imageDestroy($image_dst);
?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER

Good idea, i just tried it and I received this message:

The image “http://localhost/nixwaters/thumbs.php?file=C%2B%2B-cert-web.jpg” cannot be displayed, because it contains errors.

So, what does that tell me in the end ?
(if I load the image as original and on it's own, there is no such problem
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER

>>> Try placing comment // before the header('Content-type' line.

I did. No change in the result, but now I get a whole sequence of various
php warnings and notices reported on screen when I type the image URL directly.


>>> Do a PHPinfo() and go through it to see if you have GDlib installed

calling my test.php (phpinfo()) says that GD support is enabled, GD type bundled (2.0.28 compatible)
with the whole list for possible image filetypes reported as enabled, too.


Avatar of xberry

ASKER

By the way, if anyone would know of any smart alternative way of creating thumbnail previews . . .
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER


Notice: Undefined variable: file in /srv/www/htdocs/nixwaters/thumbs.php on line 10

Notice: Use of undefined constant image_src - assumed 'image_src' in /srv/www/htdocs/nixwaters/thumbs.php on line 20

Notice: Undefined variable: image_src in /srv/www/htdocs/nixwaters/thumbs.php on line 23

Warning: imagesx(): supplied argument is not a valid Image resource in /srv/www/htdocs/nixwaters/thumbs.php on line 23

Notice: Undefined variable: image_src in /srv/www/htdocs/nixwaters/thumbs.php on line 24

Warning: imagesy(): supplied argument is not a valid Image resource in /srv/www/htdocs/nixwaters/thumbs.php on line 24

Notice: Undefined variable: image_src in /srv/www/htdocs/nixwaters/thumbs.php on line 41

Warning: imagecopyresized(): supplied argument is not a valid Image resource in /srv/www/htdocs/nixwaters/thumbs.php on line 41

Notice: Undefined variable: image_src in /srv/www/htdocs/nixwaters/thumbs.php on line 44

Warning: imagedestroy(): supplied argument is not a valid Image resource in /srv/www/htdocs/nixwaters/thumbs.php on line 44
Avatar of xberry

ASKER

I have to apologize, on base of those warnings I just realized a typing mistake in my source code.
Now the code seems free of syntactic errors, but still no thumbnail image displayed.
I thought I put the 'clean' code again, so that you may directly check that one.

<?php
/* setting variables */
if (isset($_GET['file'])) {
$src_file = $_GET['file'];
$max_px = 120;
$dst_w = 0;
$dst_h = 0;

        /* open depending on filetype */
        $pos = strrpos($src_file, '.') +1;
        $file_ext = substr($src_file, $pos);
        switch(strToLower($file_ext)) {
                case 'png' :            $image_src = imageCreateFromPng($src_file);
                        break;
                case 'jpg' :  
                case 'jpeg':            $image_src = imageCreateFromJpeg($src_file);
                        break;
        }
                                                                   
        /* get size of image */
        if (! $image_src) {
                echo "jpg wurde nicht geladen.";
        } else {
        $src_w = imagesx($image_src);
        $src_h = imagesy($image_src);
        }
       
        /* set size of thumbnail */
        if ($src_w > $src_h) {
                $dst_w = $max_px;
                $dst_h = $max_px / ($src_w / $src_h);
        } else if ($src_w < $src_h) {
                $dst_h = $max_px;
                $dst_w = $max_px / ($src_h / $src_w);
        } else {
                $dst_h = $max_px;
                $dst_w = $max_px;
        }
               
        /* copy and edit image */
        $image_dst = imageCreateTrueColor($dst_w, $dst_h);
        imageCopyResized($image_dst, $image_src, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        //header('Content-Type: image/jpeg');
        imagejpeg($image_dst, $src_file);
        imageDestroy($image_src);
        imageDestroy($image_dst);
} else {
        echo "image file not set.";
}
?>

As you can see, I am now also checking, if images file is really set, but don't get an warning echoed.


Also I rise the points to maximum, since this doesn't seem so easy and is really bugging me.
Avatar of xberry

ASKER

If I uncomment the header line, I still get the same error that I had before
(image cannot be displayed because it contains errors)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER

Thanks ThG, seems we getting closer to a solution:

Now I do have at least a black square on screen.

Interesting that a square form I'd only get if width and height of
the else condition regarding size could not be retrieved or, being same,
but since my original is a rectangle in 'portrait' format, higher than wide,
it seems to me that there is a problem retrieving size information.
 
Avatar of xberry

ASKER

Just tried a different, more compact script that I took from the PHP manual pages,
but same result, a black square. I don't think the problem is the script, but
maybe the jpeg file itself. I did read that in certain cases jpeg headers
cannot be processed by certain versions of PHP.  I did process the images
through the GIMP image manipulation programm and maybe there is some information,
that cannot be handled.

If I comment the header line again, I get some garbage (jpg-binary code likely)
from which only those lines are readable and copyable

JFIF . . . - garbage - . . . CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality . . . - garbage - . . . .  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER

Thanks for your last comment.

News on this one:

I'm using the script below now, focussing on jpeg files
and using names that do not require to urlencode.
I came to an interesting result:

with imagejpeg() I actually now give a seperate destination for the created thumbnail,
to see what the result looks like and actually, when loading my "thumbtest.html" into browser
(with the  <img src="newthumb.php?file=filename.jpeg">
it now does create a real thumbnail of my original and saves it in images/thumbs/
ONLY . . . it does not appear on the browser unless I put another img tag in "thumbtest.html", such
as <img src="images/thumbs/thumb_filename.jpeg"> which actually loads the stored thumb
that has been created by the img tag earlier on. So in my html-file it looks like:
                <img src="newthumb.php?file=filename.jpeg">
                <img src="images/thumbs/thumb_filename.jpeg">

So, the thumbnail-functionality as such is working but originally I wanted to create
a temporay thumbnail for screen, loading it with only one <img> tag for that purpose.
would that be possible and how would I have to modify my PHP-script
for that purpose ?


<?php
/* setting variables */
if (isset($_GET['file'])) {
        $thumbsize = 120;
        $imgfile = $_GET['file'];
        header('Content-Type: image/jpeg');
        list($width, $height) = getimagesize($imgfile);
        $imgratio=$width/$height;
        if ($imgratio>1) {
                $newwidth = $thumbsize;
                $newheight = $thumbsize/$imgratio;
        } else {
                $newheight = $thumbsize;
                $newwidth = $thumbsize*$imgratio;
        }
        $thumb = ImageCreateTrueColor($newwidth, $newheight);
        $source = imagecreatefromjpeg($imgfile);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($thumb, "images/thumbs/thumb_$imgfile", 100);
        imageDestroy($imgfile);
        imageDestroy($thumb);
} else {
echo "imagefile could not be loaded.";
}
     
Thanks !
Avatar of xberry

ASKER

Sorry ThG,

I just did remember this one form comment above:

>>  imagejpeg($image_dst, $src_file);
>>  this will actually output the image to $src_file, and I don't think you want this?
>>  just imagejpeg($image_dst);, including the above commented-out header.

Now it is actually working fine !!!

So, it looked as if the problem was with the '++' characters in that special
file above. I noticed it when I opened with my file browser that despite the
urlencoded name it couldn't handle that when being reread by the php-methods, because it appeared as
C  -cert.jpeg (with double space) when it saved it temporarily.

Anyway, for this purpose I can rename my sensible image files, containing no special characters.

Thanks for your assistance to you all.
Avatar of xberry

ASKER

Again thanks to you all.