Hello!

I am new into pictures in PHP.

How do I make thumbnails please?

I have seen a program, Imagemagick, http://www.imagemagick.org/

Is there any free "component" that can be installed on the server to do thumnails, or any small codescript that make it?

Please explain the basics of thumbnails.

    No point in trying to explain all this here - I recommend you do a search on 'GD' or 'image magick' on this forum. Many thread about it 🙂

      Hello, thanks for the advice. 🙂

      I found a lot on both. GD seems easier for making thumbnails.

      I do have three questions now please:

      Question1)
      Does GD has to be installed on my webhost server, to be able to use GD? Do I have to talk to the host support people?

      Question2)
      Or can I upload it as any other code?

      Question3)
      Does GD costs money, if so, is there any simular free script/program/component out there

        1) Yes.

        2) No.

        3) No. GD is distributed with the PHP distribution.

          Hello!

          Thans for the answer.

          I have found out a lot easier way to do thumbnails, I just set the values on the picture to high=100 width=100 for instance.

            no no no no no no you cant do that at all!

            That's completely the wrong way to do it: just setting the image width and height doesnt downsize the image at all: it just displays it small. That means that it will take the same time to download as it would if you left it full size, which completely misses the point of creating thumbnails.

            Either read up how to do it using php, or if you cant be bothered, then resize every image in paint shop pro or whatever you use to be 100x100, but whatever you do, do not just tell it to display the images 100x100.

              Hello jpmoriarty!

              Thanks a lot for the post, and saying how I should not do.

              I should use for instance GD for this thumbnail making then? (Have I understand it correct now?)

              BTW, what is "GD" standing for please?

              Thanks in advance. 🙂

              Klas

                Just to make things even more complicated for you "Pellefant":
                😉

                GD is in my opinion not good at handling pictures. The difference between making thumbnails with GD and ImageMagick is huge so despite the extra hazzle I always go with ImageMagick - unless I would actually have to create images from script.

                As stated before I'm sure you can easily find tutorials here and on google as to how to install ImageMagick on your server.

                  i use this code (in the file "makeimage.php") to make my thumbs:

                  <?
                        if (@!$max_width)
                          $max_width = 100;
                        if (@!$max_height)
                          $max_height = 700;
                  
                    $directory = "photos";
                    $file = $directory."/".$file;
                  
                    $size = GetImageSize($file);
                    $width = $size[0];
                    $height = $size[1];
                  
                    $x_ratio = $max_width / $width;
                    $y_ratio = $max_height / $height;
                  
                    if ( ($width <= $max_width) && ($height <= $max_height) ) {
                      $tn_width = $width;
                      $tn_height = $height;
                    }
                    else if (($x_ratio * $height) < $max_height) {
                      $tn_height = ceil($x_ratio * $height);
                      $tn_width = $max_width;
                    }
                    else {
                      $tn_width = ceil($y_ratio * $width);
                      $tn_height = $max_height;
                    }
                  
                    $src = ImageCreateFromJpeg($file);
                    $dst = ImageCreateTrueColor($tn_width,$tn_height);
                    ImageCopyResized($dst, $src, 0, 0, 0, 0,
                        $tn_width,$tn_height,$width,$height);
                    header("Content-type: image/jpeg");
                    ImageJpeg ($dst, '', 75);
                    ImageDestroy($src);
                    ImageDestroy($dst);
                  
                  ?>
                  

                  and i use it by having (on another page) the HTML line:

                  <img src="makeimage.php?file=filename.jpg">

                  I'm not sure what blr32 means by "not good at handling pictures": i use this to make thumbs, and i want them to be small so that they dont take long to download, but still be of reasonable quality. The images that this creates are ~5k, so that's fine, and they look okay, and they dont take any time at all to be created. In my mind that's fine and is therefore doing what i wanted it to: i dont see any need for me to spend even more time trying to get my hoster to install imagemagic or whatever for me.

                  But that's just me.

                    Hello!

                    Thanks for all the help.

                    I have now been in contact with my host, they have GD installed, but not ImageManick, so I proberbly have to go with GD. 🙁

                    Thanks again for all responce.

                      Thanks a lot for the code jpmoriarty, will have use of that later on.

                      happy

                      😉

                        http://expert.phptalk.com/demos/
                        now find somewhere link to ImageResizer
                        there is online demo & download link.
                        Since on my server GD2 is not fully functional quolity of
                        resized images is not so good.

                          Hi All,

                          I got the thumbnail scripts from Loris here,
                          http://www.phpbuilder.com/annotate/message.php3?id=1013332
                          and it work GREAT !!! except that, now my page is acting strangely...

                          When i first call the page for the first time, i will get broken thumbnail images. But if you HIT refresh, the thumbnail image appears again....

                          Here is the source codes

                          $conn = connect_db();
                          mysql_select_db($database, $conn);
                          $sql = "SELECT * FROM ads_images WHERE ads_image_id=".$_GET["iid"];
                          $result = mysql_query ($sql, $conn);
                          if (mysql_num_rows ($result)>0) {
                          $row = @mysql_fetch_array ($result);
                          $id = $row["ads_image_id"];
                          $type = $row["ads_image_type"];
                          $name = $row["ads_image_name"];
                          $data = $row["ads_image"];

                          }

                          // the temp file's extension changes wether it's a jpeg or png

                          switch ($type) {
                          case $type="image/jpeg":
                          $temp="temp/temp_$id.jpg";
                          $src=imagecreatefromjpeg($temp);

                          break;

                          case $type="image/gif":
                          $temp="temp/temp_$id.gif";
                          $src=imagecreatefromgif($temp);

                          break;

                          case $type="image/png":
                          $temp="temp/temp_$id.png";
                          $src=imagecreatefrompng($temp);

                          break;

                          default:

                          $temp="temp/temp_$id.jpg";

                          $src=imagecreatefromjpeg($temp);

                          }

                          // write to a temp file the image in order to imagecreate it from jpeg,png,gif

                          if (!$file=fopen($temp,"w")) return TRUE;
                          fwrite($file,$data);
                          fclose($file);

                          Header("Content-type: $type");

                          $w_src = imagesx($src); // source image width
                          $h_src = imagesy($src); // source image height

                          if (!$w) { $w = ($h/$h_src)*$w_src; } // calculate thumbnail width, given its height

                          if (!$h) { $h = ($w/$w_src)*$h_src; } // calculate thumbnail height, given its width

                          $img = imagecreatetruecolor($w,$h); // i use imagecreatetruecolor to keep a high quality img

                          imagecopyresampled($img,$src,0,0,0,0,$w,$h,$w_src,$h_src);
                          imagejpeg($img,'',85); // the third param. represents the thumbnail quality from 1 to 100

                          unlink($file);
                          unset($sql);
                          unset($temp);
                          imagedestroy($img);
                          imagedestroy($src);
                          mysql_free_result($result);
                          mysql_close($db);
                          ?>

                          I am calling this function in another page...
                          Please take a look here..

                          http://www.1800-cars-for-sale.com/staging/kl/detail3.php?did=47

                          Notice the first time, you may encounter a broken image, HIT refresh...u will see the image download again...

                          OR is it just CACHE ??? i try with other pc is still the same problem.
                          Hope someone can help....

                          thks alot !!!
                          Pacochin

                            Write a Reply...