Example #1
0
function GenerateThumbFile($from_name, $to_name, $n_wid, $n_hgt)
{
    $max_x = $n_wid;
    $max_y = $n_hgt;
    $cut_x = 0;
    $cut_y = 0;
    $save_to_file = true;
    $image_type = -1;
    $quality = 100;
    // if src is URL then download file first
    $temp = false;
    if (substr($from_name, 0, 7) == 'http://') {
        $tmpfname = tempnam("tmp/", "TmP-");
        $temp = @fopen($tmpfname, "w");
        if ($temp) {
            @fwrite($temp, @file_get_contents($from_name)) or die("Cannot download image");
            @fclose($temp);
            $from_name = $tmpfname;
        } else {
            die("Cannot create temp file");
        }
    }
    // check if file exists
    if (!file_exists($from_name)) {
        die("Source image does not exist!");
    }
    // get source image size (width/height/type)
    // orig_img_type 1 = GIF, 2 = JPG, 3 = PNG
    list($orig_x, $orig_y, $orig_img_type, $img_sizes) = @GetImageSize($from_name);
    // cut image if specified by user
    if ($cut_x > 0) {
        $orig_x = min($cut_x, $orig_x);
    }
    if ($cut_y > 0) {
        $orig_y = min($cut_y, $orig_y);
    }
    // should we override thumb image type?
    $image_type = $image_type != -1 ? $image_type : $orig_img_type;
    // check for allowed image types
    if ($orig_img_type < 1 or $orig_img_type > 3) {
        die("Image type not supported");
    }
    if ($orig_x > $max_x or $orig_y > $max_y) {
        // resize
        $per_x = $orig_x / $max_x;
        $per_y = $orig_y / $max_y;
        if ($per_y > $per_x) {
            $max_x = $orig_x / $per_y;
        } else {
            $max_y = $orig_y / $per_x;
        }
    } else {
        // keep original sizes, i.e. just copy
        if ($save_to_file) {
            @copy($from_name, $to_name);
        } else {
            switch ($image_type) {
                case 1:
                    header("Content-type: image/gif");
                    readfile($from_name);
                    break;
                case 2:
                    header("Content-type: image/jpeg");
                    readfile($from_name);
                    break;
                case 3:
                    header("Content-type: image/png");
                    readfile($from_name);
                    break;
            }
        }
        return;
    }
    if ($image_type == 1) {
        // should use this function for gifs (gifs are palette images)
        $ni = imagecreate($max_x, $max_y);
    } else {
        // Create a new true color image
        $ni = ImageCreateTrueColor($max_x, $max_y);
    }
    // Fill image with white background (255,255,255)
    $white = imagecolorallocate($ni, 255, 255, 255);
    imagefilledrectangle($ni, 0, 0, $max_x, $max_y, $white);
    // Create a new image from source file
    $im = ImageCreateFromType($orig_img_type, $from_name);
    // Copy the palette from one image to another
    imagepalettecopy($ni, $im);
    // Copy and resize part of an image with resampling
    imagecopyresampled($ni, $im, 0, 0, 0, 0, $max_x, $max_y, $orig_x, $orig_y);
    // srcW, srcH
    // save thumb file
    SaveImage($ni, $to_name, $image_type, $save_to_file, $quality);
    if ($temp) {
        unlink($tmpfname);
        // this removes the file
    }
}
function GenerateThumbFile($from_name, $to_name, $max_x, $max_y)
{
    global $save_to_file, $image_type, $image_quality;
    $image_flag = 0;
    $resize_flag = 0;
    // get source image size (width/height/type)
    // orig_img_type 1 = GIF, 2 = JPG, 3 = PNG
    list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($from_name);
    /*echo "original x =".$orig_x."<br>";
    	  echo "original y =".$orig_y."<br>";
    	  echo "image sizes =".$img_sizes."<br>";
    
    	  echo "max x =".$max_x."<br>";
    	  echo "max y =".$max_y."<br>";
    
    	  exit;*/
    //$a = GetImageSize($from_name);
    // should we override thumb image type?
    $image_type = $image_type != -1 ? $image_type : $orig_img_type;
    // check for allowed image types
    // if ($orig_img_type < 1 or $orig_img_type > 3) die("Image type not supported");
    if ($orig_x < $max_x and $orig_y < $max_y) {
        $resize_flag = 1;
        //header('Location:'.$_SERVER['HTTP_REFERER']);
        // exit;
        //return $resize_flag;
    }
    if ($resize_flag != 1) {
        if ($orig_x > $max_x or $orig_y > $max_y) {
            /*-------if original image size is less than the specified size to resize no need to resize------------- */
            /*$per_x = $orig_x / $max_x;
            		$per_y = $orig_y / $max_y;
            		if ($per_y > $per_x)
            		{
            		  $max_x = $orig_x / $per_y;
            		}
            		else
            		{
            		  $max_y = $orig_y / $per_x;
            		}*/
        } else {
            // keep original sizes, i.e. just copy
            if ($save_to_file) {
                @copy($from_name, $to_name);
                // echo "testing";
                //exit;
            } else {
                switch ($image_type) {
                    case 1:
                        header("Content-type: image/gif");
                        include $from_name;
                        break;
                    case 2:
                        header("Content-type: image/jpeg");
                        include $from_name;
                        break;
                    case 3:
                        header("Content-type: image/png");
                        include $from_name;
                        break;
                }
            }
            return;
        }
        //if($resize_flag == 0)
        //{
        if ($image_type == 1) {
            // should use this function for gifs (gifs are palette images)
            $ni = imagecreate($max_x, $max_y);
            //echo "1ni=".$ni;
            //exit;
        } else {
            // Create a new true color image
            $ni = ImageCreateTrueColor($max_x, $max_y);
            //echo "2ni=".$ni;
            //exit;
        }
        // Fill image with white background (255,255,255)
        $white = imagecolorallocate($ni, 255, 255, 255);
        imagefilledrectangle($ni, 0, 0, $max_x, $max_y, $white);
        // Create a new image from source file
        $im = ImageCreateFromType($orig_img_type, $from_name);
        if (!$im) {
            $image_flag = 1;
        } else {
            //imagepng($im);
            // Copy the palette from one image to another
            imagepalettecopy($ni, $im);
            // Copy and resize part of an image with resampling
            imagecopyresampled($ni, $im, 0, 0, 0, 0, $max_x, $max_y, $orig_x, $orig_y);
            // srcW, srcH
            // save thumb file
            SaveImage($image_type, $ni, $to_name, $image_quality, $save_to_file);
            // }
        }
    }
    return $image_flag;
}