Page 1 of 1

PHP thumbnail help

Posted: Wed Jul 28, 2004 10:44 pm
by khawarikhan
Can someone please help me with a little problem I am having. I want to generate thumbnails for the images uploaded on my site. I want to auto generate the thumbnail for the images that I upload to my site. In the admin area for my site, I have an option to upload image for my products. Now, rahter than converting the image to the thumbnail manually using somesoftware and then upload both image & its thumbnail, I want to upload the image only to the PHP site and the code will generate the thumnail for me. Thanks for your help!!!!!!!

Khawar

Posted: Thu Jul 29, 2004 3:01 am
by Pills
As this has nothing to do with EFnet, moved to Off-Topic.

Posted: Thu Jul 29, 2004 7:39 pm
by munky
i'll leave it to you to fill in the gaps, but this is how we do thumbnailing:

Code: Select all

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      $LARGE_IMG_SIZE = 320;
      $SMALL_IMG_SIZE = 96;
      error_reporting(53);
      $acceptedTypes = array('image/jpeg', 'image/jpg', 'image/pjpeg');
      if(in_array($_FILES['user_img']['type'], $acceptedTypes) && trim($_FILES['user_img']['tmp_name']) != "")
      {
        $img_orig_size = getimagesize($_FILES['user_img']['tmp_name']);
        $img_orig_width = $img_orig_size[0];
        $img_orig_height = $img_orig_size[1];

        $img_original = ImageCreateFromJpeg($_FILES['user_img']['tmp_name']);
        $image_stored = $_SESSION['user_id'];

        print "$img_orig_width $img_orig_height $image_stored<br>";
        if($img_orig_width > $LARGE_IMG_SIZE || $img_orig_height > $LARGE_IMG_SIZE)
        {
          $mlt_w = $LARGE_IMG_SIZE / $img_orig_width;
          $mlt_h = $LARGE_IMG_SIZE / $img_orig_height;
          $mlt = $mlt_w < $mlt_h ? $mlt_w : $mlt_h;
          $img_new_width = round($img_orig_width * $mlt);
          $img_new_height = round($img_orig_height * $mlt);

          $img_resized = imagecreatetruecolor($img_new_width, $img_new_height);
          imagecopyresampled($img_resized, $img_original, 0 , 0 , 0 , 0, $img_new_width, $img_new_height, $img_orig_width, $img_orig_height);

          $BIGimg_name = $image_stored."l.jpg";
          Imagejpeg($img_resized, "$IMG_ROOT/$BIGimg_name");
        }
        else
        {
          $BIGimg_name = $image_stored."l.jpg";
          Imagejpeg($img_original, "$IMG_ROOT/$BIGimg_name");
        }

        if($img_orig_width > 96 || $img_orig_height > 96)
        {
          $mlt_w = $SMALL_IMG_SIZE / $img_orig_width;
          $mlt_h = $SMALL_IMG_SIZE / $img_orig_height;
          $mlt = $mlt_w < $mlt_h ? $mlt_w : $mlt_h;
          $img_new_width = round($img_orig_width * $mlt);
          $img_new_height = round($img_orig_height * $mlt);

          $img_resized = imagecreatetruecolor($img_new_width, $img_new_height);
          imagecopyresampled($img_resized, $img_original, 0 , 0 , 0 , 0, $img_new_width, $img_new_height, $img_orig_width, $img_orig_height);

          $SMALLimg_name = $image_stored."s.jpg";
          Imagejpeg($img_resized, "$IMG_ROOT/$SMALLimg_name");
        }
        else
        {
          $SMALLimg_name = $image_stored."s.jpg";
          Imagejpeg($img_original, "$IMG_ROOT/$SMALLimg_name");
        }
        ImageDestroy($img_resized);
      }