Solved

PHP filename for thumbnail in src of img tag ?

Posted on 2006-06-17
19
445 Views
Last Modified: 2012-05-05
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 ?
0
Comment
Question by:xberry
  • 12
  • 3
  • 2
  • +1
19 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 200 total points
ID: 16926388
You should url_encode your filename I think:

<img src="thumbs.php?file=C%2B%2B-cert-web.jpg" />

-r-
0
 
LVL 14

Author Comment

by:xberry
ID: 16926642
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);
?>
0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 200 total points
ID: 16926688
What happens when you type in the image url in your browser location bar? Does it load, or does it show any errors/warnings?

-r-
0
 
LVL 14

Author Comment

by:xberry
ID: 16926738
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
0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 200 total points
ID: 16926819
Try placing comment // before the header('Content-type' line.

-r-
0
 
LVL 15

Assisted Solution

by:JakobA
JakobA earned 100 total points
ID: 16928536
Your script uses GDlib for imagemanipulation and GDlib is not one of the default php modules. You have to add it yourself.

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

Author Comment

by:xberry
ID: 16929188
>>> 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.


0
 
LVL 14

Author Comment

by:xberry
ID: 16929195
By the way, if anyone would know of any smart alternative way of creating thumbnail previews . . .
0
 
LVL 15

Assisted Solution

by:JakobA
JakobA earned 100 total points
ID: 16929230
Please cut&paste those warnings and notices in here. They probably tell why :-))
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Author Comment

by:xberry
ID: 16932733

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
0
 
LVL 14

Author Comment

by:xberry
ID: 16932823
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.
0
 
LVL 14

Author Comment

by:xberry
ID: 16932836
If I uncomment the header line, I still get the same error that I had before
(image cannot be displayed because it contains errors)
0
 
LVL 14

Assisted Solution

by:ThG
ThG earned 200 total points
ID: 16932853
       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.
0
 
LVL 14

Author Comment

by:xberry
ID: 16932896
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.
 
0
 
LVL 14

Author Comment

by:xberry
ID: 16933199
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 - . . . .  
0
 
LVL 14

Assisted Solution

by:ThG
ThG earned 200 total points
ID: 16942528
i haven't ever found a valid image that php was not able to handle.
anyway you may want to verify your sizes with http://it.php.net/getimagesize

don't worry, you can use var_dump() on your vars as long as you comment out the content-type header. You will see your debug data followed by the jpeg binary garbage. When you are done with debugging, just take out all the var_dump's and restore the content-type header.
0
 
LVL 14

Author Comment

by:xberry
ID: 16949072
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 !
0
 
LVL 14

Author Comment

by:xberry
ID: 16949209
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.
0
 
LVL 14

Author Comment

by:xberry
ID: 16949322
Again thanks to you all.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now