i hav the new dimmensions for an image and the location of the file, how do i do this.

$new_image_width
$new_image_height
$image_file_location

    i use the code below. place it in a file called thumbnail.php, and then you can use the url

    thumbnail.php?filename=YOURFILE&x=WIDTH&y=HEIGHT

    from any page, ie

    <IMG SRC="thumbnail.php?filename=me.jpg&x=100&y=100">

    also this has the benefit of not distorting the image - it will maintain the aspect ration of the image when you resize.

    here is the code

    <?php
    
    function loadjpeg($path, $max_x, $max_y) {
     $im = @imagecreatefromjpeg($path);
     if (!$im) {
       $im  = imagecreate(150, 30);
       $bgc = imagecolorallocate($im, 255, 255, 255);
       $tc  = imagecolorallocate($im, 0, 0, 0);
       imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
       imagestring($im, 1, 5, 5, "Error loading $path", $tc);
     }
     if ($max_x != 0 && $max_y != 0) {
       $x = imagesx($im);
       $y = imagesy($im);
       if ($x > $max_x) {
         $y = (int)floor($y * ($max_x / $x));
         $x = $max_x;
       }
       if ($y > $max_y) {
         $x = (int)floor($x * ($max_y / $y));
         $y = $max_y;
       }
       if (imagesx($im) != $x || imagesy($im) != $y) {
         $tmp = imagecreate($x, $y);
         imagecopyresized($tmp, $im, 0, 0, 0, 0, $x, $y, imagesx($im), imagesy($im));
         imagedestroy($im);
         $im = $tmp;
       }
     }
     imagejpeg($im);
    }
    
    $filename = $_GET['filename'];
    $width = $_GET['x'];
    $height = $_GET['y'];
    
    loadjpeg($filename, $width, $height);
    
    ?>
    

    note: if you are using GD 2.0 or above, replace imagecopyresized with imagecopyresampled, and the quality of the thumbnail will be better.

    this isn't my work, but i don't know who the credit should go to.

    adam

      thanks for the reply, but this is not quite what im looking for.

      I need a php script that can resize an actual image and then replace the original with the resized version.

        Here is a short script I made a while back which creates thumbnails in various forms depending on the input recieved via HTTP GET.

        examples:
        1) 'create_thumb.php?img=IMG1.JPG&limit_width=640&limit_height=480'
        Creates a thumbnail, preserving the aspect ratio of the original image (so it isnt squished/stretched at all) and limits the size to fitting inside a 640 x 480 pixel box.

        2) 'create_thumb.php?img=IMG1.JPG&limit_width=640&limit_height=480&stretch'
        Same as before except the image is stretched to fit exactly in a 640 x 480 box.

        Notes: The script will attempt to cache the file with the name <original_file_name>thumb<pixel_width>x<pixel_height>_<scale/stretch> unless the variable 'no_cache' or 'no_write' exists in the HTTP GET stream; if the cache is successful, then all recurring requests will be loaded from the cache file.

        example of http url which tells the script not to cache:
        'create_thumb.php?img=IMG1.JPG&limit_width=640&limit_height=480&stretch&no_cache'

        <?
        // Jon Newman - jnewman (at) oplink (d0t) net (2003)
        
        function create_thumb($im_source, $max_x, $max_y, $output_file, $stretch=FALSE)
        {
          $im_prop=getimagesize($im_source);
          if(!$im_prop[2]==2)
            return(0);
          else
          {
            $src_im=imagecreatefromjpeg($im_source);
            $src_x=imagesx($src_im);
            $src_y=imagesy($src_im);
        
        $thumb_x=$src_x;
        $thumb_y=$src_y;
        if($src_x>$max_x && $src_x>=$src_y)
        {
          $thumb_x=$max_x;
          if(!$stretch)
            $thumb_y=(int)(($max_x/$src_x)*$src_y);
          else
            $thumb_y=$max_y;
        }
        else if($src_y>$max_y && $src_y>$src_x)
        {
          $thumb_y=$max_y;
          if(!$stretch)
            $thumb_x=(int)(($max_y/$src_y)*$src_x);
          else
            $thumb_x=$max_x;
        }
        
        if(($thumb_y=="0") && ($thumb_x=="0"))
          return(0);
        elseif($thumb_y == "0")
        {
          $scale_x=$thumb_x/($src_x-1);
          $thumb_y=$src_y*$scale_x;
        }
        elseif($thumb_x=="0")
        {
          $scale_y=$thumb_y/($src_y-1);
          $thumb_x=$src_x*$scale_y;
        }
        
        $thumb_x=(int)($thumb_x);
        $thumb_y=(int)($thumb_y);
        $dest_im=imagecreatetruecolor($thumb_x, $thumb_y);
        
        if(!imagecopyresized($dest_im, $src_im, 0, 0, 0, 0, $thumb_x, $thumb_y, $src_x, $src_y))
        {
          imagedestroy($src_im);
          imagedestroy($dest_im);
          return(0);
        }
        else
        {
          imagedestroy($src_im);
          if($output_file!='')
          {
            if(@imagejpeg($dest_im,$output_file))
            {
              imagedestroy($dest_im);
              return(-1);
            }
            else
            {
              imagejpeg($dest_im);
              imagedestroy($dest_im);
              return(0);
            }
          }
          else
          {
            imagejpeg($dest_im);
            imagedestroy($dest_im);
            return(-1);
          }
          imagedestroy($dest_im);
        }
        return(0);
          }
        }
        
        header("Content-type: image/jpeg");
        if(isset($_GET['img']) && file_exists($_GET['img']))
        {
          $_file=$_GET['img'].'_thumb_'.$_GET['limit_width'].'x'.$_GET['limit_height'].(isset($_GET['stretch']) ? '_stretch' : '_scale');
        
          if(!file_exists($_file) || isset($_GET['no_cache']))
          {
            if(($t=create_thumb($_GET['img'],(isset($_GET['limit_width']) ? $_GET['limit_width'] : 100),(isset($_GET['limit_height']) ? $_GET['limit_height'] : 100),(!isset($_GET['nowrite']) && !isset($_GET['no_cache']) ? $_file : ''),(isset($_GET['stretch']) ? TRUE : FALSE)))==-1)
              imagejpeg(@imagecreatefromjpeg($_file));
            else
            {
              $im=imagecreatetruecolor(100,100);
              $background_color=imagecolorallocate($im, 0, 0, 0);
              $text_color=imagecolorallocate($im, 233, 14, 91);
              imagestring($im, 2, 35, 40,  ": X :", $text_color);
              imagejpeg($im);
            }
          }
          else
          {
            imagejpeg(@imagecreatefromjpeg($_file));
          }
        }
        else
        {
          $im=imagecreatetruecolor(100,100);
          $background_color=imagecolorallocate($im, 0, 0, 0);
          $text_color=imagecolorallocate($im, 233, 14, 91);
          imagestring($im, 2, 35, 40,  ": X :", $text_color);
          imagejpeg($im);
        }
        ?>
        

        Although this doesn't do exactly what you want you should be able to modify it to suit your needs.

          i still do not seem to be making my point clear. i need the script to OPEN a image on the server (eg. pics/001.jpg) RESIZE to a height and width i can easily specify. Once its resized it i need 2 to replace the old image. (overrite pics/001.jpg)

            This is why it's a mistake to just give people the code - they never learn from it.

            You want to open an [man]image[/man] on the server. Okay, there are functions for that: [man]imagecreatefromjpeg[/man] springs to mind.
            You want to resize the image. Right, there are functions for that: [man]imagecopyresize[/man] is the obvious one, but [man]imagecopyresampled[/man] gives the better result.

            And then you want to save it. Fine. [man]imagejpeg[/man] will do that.

            Read those pages. You will find examples of their use.

              Ok...if you want someone to write the script for you...then go somewhere else. What I gave you is 95% of what you needed and if you can't figure out how to change it to your needs...then uh....nevermind 😉

              You may think about learning how to program before actually attempting such a thing...

                well based on what functions i was told to use and those other scripts i've written this.

                <?php
                $currentimagesize = getimagesize("pics/Dane_bike_pic.jpg");
                $image_width = $currentimagesize[0];
                $image_height= $currentimagesize[1];

                //calc

                $new_image_width = 600;

                $divide_value = $image_width / $new_image_width ;
                $new_image_height = $image_height / $divide_value ;

                $bikepicimgoriginal = imagecreatefromjpeg("pics/Dane_bike_pic.jpg");
                $bikepicimgnew = imagecopyresized ( $bikepicimgnew, $bikepicimgoriginal, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
                imagejpeg($bikepicimgnew);

                ?>

                at the momment this doesnt work, i was wonderin wot do i have to do to make it work. the error messages are

                Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/dc/public_html/test.php on line 14

                Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/dc/public_html/test.php on line 15

                  If image magick is installed in the server you can try convert:
                  <?
                  $the_path ="/....../images";
                  $file_name ="test.jpg";
                  exec("convert -size 250x250 $the_path/$file_name -resize 250x250 +profile \"*\" $the_path/$file_name");
                  ?>

                    no it isnt installed on my server, but cheers for the reply.

                    So does any1 kno how i could fix my script.

                      9 days later

                      Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/dc/public_html/test.php on line 14

                      Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/dc/public_html/test.php on line 15

                      This suggests to me that the variable you are passing to these functions does not contain the path to an image

                        Write a Reply...