Locate an image file on the server, extract data, resize and then output new image - can this be done?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bizt

    Locate an image file on the server, extract data, resize and then output new image - can this be done?

    Hi,

    I have a PHP program on my webspace that I use to upload images. As the
    images are about 500KB each, I would like to create a preview thumbnail
    image with smaller image dimensions than the original.

    Now I dont know a great deal about image manipulation but heres how I
    would imagine it might be done:

    1) The image file would be located on the server from the PHP script
    (eg. show_image.php - this would be called from the <imgtag 'src'
    attribute).
    2) The image data would be extracted from the image.
    3) The image data would be resized
    4) The resized image data would be output

    Im seen something similar. Can this be done simply?

    Cheers

    Burnsy

  • Richard Levasseur

    #2
    Re: Locate an image file on the server, extract data, resize and then output new image - can this be done?


    bizt wrote:
    Hi,
    >
    I have a PHP program on my webspace that I use to upload images. As the
    images are about 500KB each, I would like to create a preview thumbnail
    image with smaller image dimensions than the original.
    >
    Now I dont know a great deal about image manipulation but heres how I
    would imagine it might be done:
    >
    1) The image file would be located on the server from the PHP script
    (eg. show_image.php - this would be called from the <imgtag 'src'
    attribute).
    2) The image data would be extracted from the image.
    3) The image data would be resized
    4) The resized image data would be output
    >
    Im seen something similar. Can this be done simply?
    >
    Cheers
    >
    Burnsy
    Yes. You use the GD library that PHP has (usually, sometimes you must
    recompile it in).

    Here's a resize script i wrote a long, long time ago. Its the first
    half of the script:


    basically, you open the file, use copyresized, then output it. Not too
    difficult. In fact, I think the PHP manual might have an example in
    it, possibly in the comments somewhere.

    Comment

    • Krustov

      #3
      Re: Locate an image file on the server, extract data, resize and then output new image - can this be done?

      <comp.lang.ph p>
      <bizt>
      <26 Jul 2006 13:00:23 -0700>
      <1153944023.139 985.255030@h48g 2000cwc.googleg roups.com>
      I have a PHP program on my webspace that I use to upload images. As the
      images are about 500KB each, I would like to create a preview thumbnail
      image with smaller image dimensions than the original.
      >

      $whatsupdoc="th umbs/image5.jpg";

      $pass=1; $filename=$what supdoc;
      if (!file_exists($ filename)) {$pass=0;}

      if ($pass==0)
      {
      $biggy="images/image5.jpg";
      $whatsupdoc="th umbs/image5.jpg";
      include('create _thumb.php');
      }


      $size=GetImageS ize($whatsupdoc );

      <img src=$whatsupdoc width=$size[0] height=$size[1] border=0>




      The above code will only create a thumbnail if it doesnt already exist .

      If the thumbnail does exist - then it will just display it .




      create_thumb.ph p


      <?php

      $img_name=$bigg y;
      $new_name=$what supdoc;
      $max_width=120;
      $max_height=70;
      $size=GetImageS ize($img_name);
      $width_ratio=($ size[0] / $max_width);
      $height_ratio=( $size[1] / $max_height);

      if($width_ratio >=$height_ratio )
      {
      $ratio = $width_ratio;
      }
      else
      {
      $ratio = $height_ratio;
      }
      $new_width=($si ze[0] / $ratio);
      $new_height=($s ize[1] / $ratio);
      $src_img=ImageC reateFromJPEG($ img_name);
      $thumb=ImageCre ateTrueColor($n ew_width,$new_h eight);
      ImageCopyResamp led($thumb,$src _img,0,0,0,0,($ new_width-1),($new_height-
      1),$size[0],$size[1]);
      ImageJPEG($thum b,$new_name);
      ImageDestroy($s rc_img);
      ImageDestroy($t humb);

      ?>





      --
      Encrypted email address

      Make a shorter url

      Comment

      • bizt

        #4
        Re: Locate an image file on the server, extract data, resize and then output new image - can this be done?

        ImageJPEG($thum b, $new_name);

        How does this work? Will this create the new thumbnail even if the file
        doesnt already exist? Its just Im getting the following error:

        Warning: imagejpeg(): Unable to open 'thumbs/image12.jpg' for writing
        in /home/martyn69/public_html/photos/photos/thumb.php on line 55


        Note: I have changed the $new_name to tie up with my 'id' field of the
        table that the image filenames are stored.

        However, when I do the following:


        header ("Content-type: image/jpeg");
        ImageJPEG ($thumb);


        ... it does output the resized image but ofcourse does not create the
        file. I would like the option of creating the file as this will surely
        reduce server processing (and faster pages). Does something need
        configured on the server (to allow write/read etc - the 'thumb' folder
        permissions are set to 777) or is my understanding of this incorrect?
        Thanks

        Burnsy

        Comment

        • Rik

          #5
          Re: Locate an image file on the server, extract data, resize and then output new image - can this be done?

          bizt wrote:
          >ImageJPEG($thu mb, $new_name);
          >
          How does this work? Will this create the new thumbnail even if the
          file doesnt already exist? Its just Im getting the following error:
          >
          Warning: imagejpeg(): Unable to open 'thumbs/image12.jpg' for writing
          in /home/martyn69/public_html/photos/photos/thumb.php on line 55
          >
          Note: I have changed the $new_name to tie up with my 'id' field of the
          table that the image filenames are stored.
          >
          Mkae sure the directory is writable for the script. CHMOD 0777

          Grtz,
          --
          Rik Wasmus


          Comment

          • bizt

            #6
            Re: Locate an image file on the server, extract data, resize and then output new image - can this be done?

            ImageJPEG($thum b, $new_name);

            Im getting different results in two scenarios, both not what I want.

            1) When I try:

            ImageJPEG($thum b, $new_name);

            ... and the new_name file doesnt exist, I get the following error:

            Warning: imagejpeg(): Unable to open 'thumbs/image68.jpg' for writing
            in /home/martyn69/public_html/photos/photos/thumb.php on line 55

            2) When I try the same code but this time upload an image that has the
            same name as new_name in the correct directory and then call thumb.php,
            it seems to run without glitch and then redirects me in the browser to
            the new_name file. Sadly, however, the new_name file has not been
            overwritten and is still the file I uploaded.


            Now, scenario 2) would not be ideal because the file would not exist as
            that is why it is having to generate the thumb for the first time. I
            just wanted to test it that way to check the behaviour.

            Anyway, here is the code of thumb.php:

            <?php

            //this is the attribute that is used to get the filename from a db
            table
            $photo_id = $_GET["photo_id"];

            //check if file exists, if so output that one. if not, generate new one
            and (hopefully) save new thumb
            $new_name = "thumbs/image" . $photo_id . ".jpg";
            if (file_exists ($new_name))
            {
            header ("Location: " . $new_name);
            }




            //file does not exist so the image will need to be resized and created
            else
            {
            include_once ("../inc/utils.php"); //contains db_conn()

            $db = db_conn ("martyn69_phot os");
            $select = "SELECT filename FROM photos WHERE photo_id = " . $photo_id;
            $result = mysql_query ($select, $db);

            $img_name = mysql_result ($result, "filename") ;



            //IMAGE GENERATE CODE

            $max_width = 100;
            $max_height = 100;
            $size = GetImageSize ($img_name);

            $width_ratio = ($size[0] / $max_width);
            $height_ratio = ($size[1] / $max_height);

            if ($width_ratio >= $height_ratio)
            {
            $ratio = $width_ratio;
            }
            else
            {
            $ratio = $height_ratio;
            }

            $new_width = ($size[0] / $ratio);
            $new_height = ($size[1] / $ratio);

            $src_img = ImageCreateFrom JPEG ($img_name);
            $thumb = ImageCreateTrue Color ($new_width, $new_height);
            ImageCopyResamp led ($thumb, $src_img, 0, 0, 0, 0, ($new_width-1),
            ($new_height-1), $size[0], $size[1]);

            //header ("Content-type: image/jpeg");
            //ImageJPEG ($thumb);

            ImageJPEG ($thumb, $new_name);

            //destroy
            ImageDestroy ($src_img);
            ImageDestroy ($thumb);
            }

            ?>

            I have ensured permissions are set to 777. I have in the past had to
            contact my web host guys to allow me to write to dir even tho they were
            set at 777, is this the case here or is my code faulty? Cheers

            Burnsy

            Comment

            Working...