so im pulling an error:

Warning: imagegif() [function.imagegif]: Unable to open 'megaphone.gif' for writing in /var/www/htdocs/~scripts/image_resize/index.php on line 42

after that i get an output of "1"

CODE:

<?php
function resizeIMG($filename, $width) {

   // Get new dimensions
   list($width_orig, $height_orig) = getimagesize($filename);
   $height = (int) (($width / $width_orig) * $height_orig);

   // Resample
   $image_p = imagecreatetruecolor($width, $height);


$format = strtolower(substr(strrchr($filename,"."),1));
  switch($format)
  {
   case 'gif' :
   $type ="gif";
   $image = imagecreatefromgif($filename);
   break;
   case 'png' :
   $type ="png";
   $image = imagecreatefrompng($filename);
   break;
   case 'jpg' :
   $type ="jpg";
   $image = imagecreatefromjpeg($filename);
   break;
   case 'jpeg' :
   $type ="jpg";
   $image = imagecreatefromjpeg($filename);
   break;
   default :
   die ("ERROR; UNSUPPORTED IMAGE TYPE");
   break;
  }

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

   // Output

  if($type=="gif")
  {
   imagegif($image_p, $filename);
  }
  elseif($type=="jpg")
  {
   imagejpeg($image_p, $filename);
  }
  elseif($type=="png")
  {
   imagepng($image_p, $filename);
  }
  elseif($type=="bmp")
  {
   imagewbmp($image_p, $filename);
  }

   return true;
}
echo resizeIMG('megaphone.gif',100);

?>

but i dont understand b/c i do not want to actually write any image files... it should all be on the fly

    The second argument to "imagejpeg()" etc. is optional. If it's supplied and is a valid filename, then the image is written to the file. If it's absent, the image is output to the browser. You're providing the original image filename as if to overwrite it with the resized image. So if you don't want to write any image files, don't supply the filenames.

    The reason you're seeing "1" as the output is that your function returns true. You call the function by echoing it, and the true is cast to string "1".

    So to get the resized image to display, change the "imagejpeg()" etc. functions to take only the first argument, and call the function without the "echo'. (Those are two things that stand out, anyway.)

      ok... revised

      <?php
      function resizeIMG($filename, $width) {
      
         // Get new dimensions
         list($width_orig, $height_orig) = getimagesize($filename);
         $height = (int) (($width / $width_orig) * $height_orig);
      
         // Resample
         $image_p = imagecreatetruecolor($width, $height);
      
         $format = strtolower(substr(strrchr($filename,"."),1));
         switch($format) {
         case 'gif' :
         $type ="gif";
         $image = imagecreatefromgif($filename);
         break;
         case 'png' :
         $type ="png";
         $image = imagecreatefrompng($filename);
         break;
         case 'jpg' :
         $type ="jpg";
         $image = imagecreatefromjpeg($filename);
         break;
         case 'jpeg' :
         $type ="jpg";
         $image = imagecreatefromjpeg($filename);
         break;
         default :
         die ("ERROR; UNSUPPORTED IMAGE TYPE");
         break;
        }
      
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
      
         // Output
        if($type=="gif")
        {
         imagegif($image_p);
        }
        elseif($type=="jpg")
        {
         imagejpeg($image_p);
        }
        elseif($type=="png")
        {
         imagepng($image_p);
        }
        elseif($type=="bmp")
        {
         imagewbmp($image_p);
        }
      }
      resizeIMG('megaphone.gif',100);
      ?>
      

      that is my index.php... now when i just load that i get a garbled screen as if I opened the image in notepad.... how do i handle the image headers on the fly?

        First assure that you don't have any empty lines / spaces / tabs outside <?php ?> tags.
        Second, you can send image headers using php function:

        header("Content-Type: image/jpeg");

        before output jpeg image. The same headers is for other formats,
        but with different MIME types ("image/gif","image/jpeg","image/png","image/bmp")

          Try adding this right after your switch block:

          header('Content-Type: image/' . $type);

            ok so now i have:

            ...
            }
             header('Content-Type: image/' . $type); 
            resizeIMG('megaphone.gif',100);
            ?>
            

            and when i load the page firefox presents me with a download box and tells me im trying to download a file type "image/ "

              Well, I said put it after your switch block. Where you have it, "$type" isn't even defined yet; it's not even in the same scope.

              This works for me:

              resizeIMG($picture, $new_width);
              
              function resizeIMG($filename, $width) 
              {
                  list($width_orig, $height_orig) = getimagesize($filename);
                  $height = (int) (($width / $width_orig) * $height_orig); 
                  $image_p = imagecreatetruecolor($width, $height);   
              $format = strtolower(substr(strrchr($filename, '.'), 1)); switch($format) { case 'gif': $type = 'gif'; $image = imagecreatefromgif($filename); break; case 'png': $type = 'png'; $image = imagecreatefrompng($filename); break; case 'jpg': case 'jpeg': $type = 'jpg'; $image = imagecreatefromjpeg($filename); break; default: die ('ERROR; UNSUPPORTED IMAGE TYPE'); break; } header('Content-Type: image/' . $type); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); if($type == 'gif') { imagegif($image_p); } elseif ($type == 'jpg') { imagejpeg($image_p); } elseif ($type == 'png') { imagepng($image_p); } elseif ($type == 'bmp') { imagewbmp($image_p); } fclose($fp); return true; }

                total brain fart on my behalf... im workin this file remotely in PICO and im used to DW coloring and such... yea totally had that header outside the function call

                that works GREAT! thanks installer for all your help and thanks to everyone else for your suggestions!

                  actuall... lol im having another issue

                  <?php
                  function resizeIMG($filename, $width)
                  {
                      $max_width='600';
                      list($width_orig, $height_orig) = getimagesize($filename);
                      $height = (int) (($width / $width_orig) * $height_orig);
                      $format = strtolower(substr(strrchr($filename, '.'), 1));
                      switch($format) {
                          case 'gif':
                              $type = 'gif';
                              $image = imagecreatefromgif($filename);
                              break;
                          case 'png':
                              $type = 'png';
                              $image = imagecreatefrompng($filename);
                              break;
                          case 'jpg':
                          case 'jpeg':
                              $type = 'jpg';
                              $image = imagecreatefromjpeg($filename);
                              break;
                          default:
                              die ('ERROR; UNSUPPORTED IMAGE TYPE');
                              break;
                      }
                      $image_p = imagecreatetruecolor($width, $height);
                      header('Content-Type: image/' . $type);
                  
                  if ($width_org <= $max_width)
                  {
                    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width_orig, $height_orig, $width_orig,
                    $height_orig);
                      }
                      else
                      {
                        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig,
                    $height_orig);
                      }
                  
                  if($type == 'gif') {
                      imagegif($image_p);
                  }
                  elseif ($type == 'jpg') {
                      imagejpeg($image_p);
                  }
                  elseif ($type == 'png') {
                      imagepng($image_p);
                  }
                  elseif ($type == 'bmp') {
                      imagewbmp($image_p);
                  }
                  fclose($fp);
                  return true;
                  }
                  resizeIMG('megaphone.gif',10);
                  ?>
                  

                  i added a caveat such that if the image is at or below a certain width already then dont resize it

                  so i added the $max_width variable and added some control on the bottom but i get this from my browser now

                  The image “XXXXXX/~scripts/image_resize/” cannot be displayed, because it contains errors.

                    There's a typo in this line:

                    if ($width_org <= $max_width)

                    #2001
                    Wine and cheese in the pod bay.

                      BTW, for jpeg you produce "image/jpg" type, but there is no such MIME type.
                      Correct is "image/jpeg".
                      And you can safely delete the following lines:

                          fclose($fp); 
                          return true; 
                      

                        stupid typos!
                        its still def not workin tho

                        <?php
                        function resizeIMG($filename, $width)
                        {
                            $max_width='600';
                            list($width_orig, $height_orig) = getimagesize($filename);
                            $height = (int) (($width / $width_orig) * $height_orig);
                            $format = strtolower(substr(strrchr($filename, '.'), 1));
                            switch($format) {
                                case 'gif':
                                    $type = 'gif';
                                    $image = imagecreatefromgif($filename);
                                    break;
                                case 'png':
                                    $type = 'png';
                                    $image = imagecreatefrompng($filename);
                                    break;
                                case 'jpg':
                                case 'jpeg':
                                    $type = 'jpeg';
                                    $image = imagecreatefromjpeg($filename);
                                    break;
                                default:
                                    die ('ERROR; UNSUPPORTED IMAGE TYPE');
                                    break;
                            }
                            header('Content-Type: image/' . $type);
                            if ($width_orig <= $max_width)
                            {
                                #$image_p = ???????
                            }
                            else
                            {
                                $image_p = imagecreatetruecolor($width, $height);
                                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                            }
                        
                        if($type == 'gif') {
                            imagegif($image_p);
                        }
                        elseif ($type == 'jpeg') {
                            imagejpeg($image_p);
                        }
                        elseif ($type == 'png') {
                            imagepng($image_p);
                        }
                        elseif ($type == 'bmp') {
                            imagewbmp($image_p);
                        }
                        }
                        resizeIMG('megaphone.gif',100);
                        ?>
                        

                        what can i set $image_p to in the first control so that is uses the original image for output instead of creating a new image?

                          Try

                          imagecopy($image_p,$image,0,0,0,0,$width_orig,$height_orig);
                          

                          The new image will be created anyway, but it should works.

                            <?php
                            function resizeIMG($filename, $width)
                            {
                                $max_width='600';
                                list($width_orig, $height_orig) = getimagesize($filename);
                                $height = (int) (($width / $width_orig) * $height_orig);
                                $format = strtolower(substr(strrchr($filename, '.'), 1));
                                switch($format) {
                                    case 'gif':
                                        $type = 'gif';
                                        $image = imagecreatefromgif($filename);
                                        break;
                                    case 'png':
                                        $type = 'png';
                                        $image = imagecreatefrompng($filename);
                                        break;
                                    case 'jpg':
                                    case 'jpeg':
                                        $type = 'jpeg';
                                        $image = imagecreatefromjpeg($filename);
                                        break;
                                    default:
                                        die ('ERROR; UNSUPPORTED IMAGE TYPE');
                                        break;
                                }
                                header('Content-Type: image/' . $type);
                                if ($width_orig <= $max_width)
                                {
                                    $image_p = imagecreatetruecolor($width_orig, $height_orig);
                                    imagecopy($image_p,$image,0,0,0,0,$width_orig,$height_orig);
                                }
                                else
                                {
                                    $image_p = imagecreatetruecolor($width, $height);
                                    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                                }
                            
                            if($type == 'gif') {
                                imagegif($image_p);
                            }
                            elseif ($type == 'jpeg') {
                                imagejpeg($image_p);
                            }
                            elseif ($type == 'png') {
                                imagepng($image_p);
                            }
                            elseif ($type == 'bmp') {
                                imagewbmp($image_p);
                            }
                            }
                            resizeIMG('megaphone.gif',100);
                            ?>
                            

                            seems to be working

                              yup.... works AWESOME! thats DEF getting saved in my library... thanks a ton guys

                                Write a Reply...