In this instance I think itd help though, becuase Im trying to make a avatar script- ie people upload their own images, n people use gifs a lot.

Im really stuck on the image create/ saving portion of this- I can resize so I spose I could set it up so images resize onload, but Id really rather have the smaller version saved on ftp- itd help load times a lot. I keep finding scripts that claim to do it but I cant get any of them to work as are, let alone edit them up to be what I need. Anyone know where I can find a tutorial in this area? Have googled it but nothing much comes up. :bemused:

    Ok Ive added a bit more, the following doesnt throw an error but it doesnt work either:

    //vars
    $filename = $_FILES['imagefile']['name'];
    $path = "users/$folder/$username/";
    $full = "users/$folder/$username/$filename";
    
    // Get image dimensions
    $imagesize = getimagesize($full);
    if ($imagesize[0] > 350) {
            $newwidth = 350; $newheight = ((350 / $imagesize[0]) * $imagesize[1]);
    
       //find the file extension
       $image_type = strstr($filename, '.');
    
       //decide which imagecreate line to use
       if ($image_type == '.jpg' || $image_type == '.jpeg') {
               $source = imagecreatefromjpeg($full);
       }
        elseif ($image_type == '.gif') {
               $source = imagecreatefromgif($full);
        }
         elseif ($image_type == '.png') {
              $source = imagecreatefrompng($full);
        }
        elseif ($image_type != '.jpg' || $image_type != '.jpeg' || $image_type != '.gif' || $image_type != '.png') { 
                echo ("invalid file type");
                exit; }
    
    //I think Im ok up to here. After this Im really not sure what Im doing.
    
        //name the new image
        $file = $newfilename . $filename;
    
        //where to save new image
        $fullpath = $path . $file;
    
       //find size of old image
       list($width, $height) = getimagesize($full);
    
       //create new image
       $thumb = imagecreatetruecolor($newwidth, $newheight);
    
       //resize old image
       imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
        //save resized image || set quality
        imagejpeg($thumb, $fullpath, 60);
    
       //return filepath
       return $fullpath;
    }
    

      I did it! The answers actually much simpler than any of the scripts I looked at, which confuses me a little but it seems to work ok. Heres the answer if anyones interested.

      //vars
      $filename = $_FILES['imagefile']['name'];
      $path = "users/$folder/$username/";
      $full = "users/$folder/$username/$filename";
      
      // Get image dimensions
      $imagesize = getimagesize($full);
      if ($imagesize[0] > 350) {
      	$newwidth = 350; $newheight = ((350 / $imagesize[0]) * $imagesize[1]);
      
                   //THE RESIZE FUNCTION
                   //find the file extension
      $image_type = strstr($filename, '.');
      
      //decide which imagecreate line to use
      if ($image_type == '.jpg' || $image_type == '.jpeg') {
      	$source = imagecreatefromjpeg($full);
                      $create = imagejpeg;
      }
      elseif ($image_type == '.gif') {
      	$source = imagecreatefromgif($full);
      	$create = imagegif;
      }
      elseif ($image_type == '.png') {
      	$source = imagecreatefrompng($full);
      	$create = imagepng;
      }
      elseif ($image_type != '.jpg' || $image_type != '.jpeg' || $image_type != '.gif' || $image_type != '.png') { 
      	echo ("invalid file type");
      	exit; }
      
                   //image create
      $newimage = imagecreatetruecolor($newwidth, $newheight);
                  imagecopyresampled($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $imagesize[0], $imagesize[1]);
      
      // Output || path || quality
      $create($newimage, $full, 75);
      }
      

        I wouldn't trust the file extension on the filename of the uploaded image. To be a little safer, you can use the 3rd element in the output array of [MAN]getimagesize[/MAN].

        $image_type=$imagesize[2];
        switch($image_type)
        {
            case 1:
                // process as gif
                break;
            case 2:
                // process as jpg
                break;
            case 3:
                // process as png
                break;
            default;
                // if you got this far, the user uploaded an invalid mime type
                // perform clean up and exit routine
        }

          hmm.. you mean like someone could upload a evil php file and name it as a gif or something? ack. issues. ta for that. i'll change it.

            ElectricRain wrote:

            hmm.. you mean like someone could upload a evil php file and name it as a gif or something? ack. issues. ta for that. i'll change it.

            or maybe the less malicious scenario - they could upload a file that isn't the format that the file extension would suggest, and your functions will fail.

              Right. Changed it to

              
              $image_type = $imagesize[2];
              
              //decide which imagecreate line to use
              if ($image_type == '2') {
              	$source = imagecreatefromjpeg($full);
              	$create = imagejpeg;
              }
              elseif ($image_type == '1') {
              	$source = imagecreatefromgif($full);
              	$create = imagegif;
              }
              elseif ($image_type == '3') {
              	$source = imagecreatefrompng($full);
              	$create = imagepng;
              }
              elseif ($image_type != '3' || $image_type != '1' || $image_type != '2') { 
              	echo ("invalid file type");
              	exit; }
              
              

              Thanks for the tip 🙂

                Do you dislike switch statements?

                  I dont understand how to use them really. :$ why, is there a really good reason to use them instead of what I have?

                    ElectricRain wrote:

                    I dont understand how to use them really. :$ why, is there a really good reason to use them instead of what I have?

                    I guess the only benefit is it's cleaner looking and easier to read. Functionally it's the same as your if...else structure except for the fact that you can combine multiple options without say "if this or this or this...". I'd say it's worth looking into and using where applicable...I just use it where I can as a matter of preference.

                    [MAN]switch[/MAN]

                      Just remember that for a while (~v1.6-2 GD) imagecreatefromgif didn't work.

                      Basically someone copyrighted the method of compression gif uses so PHP had to remove support for it until it expired.

                      One of my projects had a thumbnail generation script that had to thumbnail gifs by just changing width and height at best :s

                      If you really want to know more on this truely boring subject google on LZW compression 😛

                        Of course there are times when if statements are the better option.

                        To quote possibly the worst use of switch I've seen (the fact that it was Java implies there were going to be issues anyway, but really someone with a PhD should know better...)

                        switch (i)
                        {
                        case 1 : return 'st'; break;
                        case 2 : return 'nd'; break;
                        case 3 : return 'rd'; break;
                        case 4 :
                        case 5 :
                        case 6 :
                        case 7 :
                        case 8 :
                        case 9 :
                        case 10 :
                        case 11 :
                        case 12 :
                        case 13 :
                        case 14 :
                        case 15 :
                        case 16 :
                        case 17 :
                        case 18 :
                        case 19 :
                        case 20 : return 'th'; break;
                        case 21 : return 'st'; break;
                        case 22 : return 'nd'; break;
                        case 23 : return 'rd'; break;
                        case 24 :
                        case 25 :
                        case 26 :
                        case 27 :
                        case 28 :
                        case 29 :
                        case 30 : return 'th'; break;
                        case 31 : return 'st'; break;
                        default : break;
                        }

                        And no, that probably won't compile but you get the idea 🙂

                        And just for anyone wondering, if () isn't the best way to do that either 😃

                          And for more advanced manipulation of gif images, there is a ton of stuff you can do with ImageMagick. For instance, I don't believe PHP has a mechanism in place for resizing animated gifs while preserving the animation. Using the methods described in the code above, PHP would flatten an animated gif to only the first frame.

                            Write a Reply...