コード例 #1
0
/**
 * generate a thumbnail
 * @param  str  $sub_path upload path
 * @param  str  $file     filename
 * @param  str  $out_file outfile name, can be null if show is true
 * @param  int  $w        desired width
 * @param  int  $h        desired max height, optional, will limit height and adjust width accordingly
 * @param  int  $quality  quality of image jpg and png
 * @param  bool $show     output to browser if true
 * @param  str $output_format optional output format, if not determining from out_file can be excusivly set (1|'GIF', 2|'JPG,'' 3|'PNG')
 * @param  boolean $upscale  true, allows image to scale up/zoom to fit thumbnail
 * @return bool            success
 */
function generate_thumbnail($file, $sub_path = '', $out_file = null, $w = null, $h = null, $crop = null, $quality = null, $show = false, $output_format = null, $upscale = false)
{
    //gd check, do nothing if no gd
    $php_modules = get_loaded_extensions();
    if (!in_arrayi('gd', $php_modules)) {
        return false;
    }
    $sub_path = tsl($sub_path);
    $upload_folder = GSDATAUPLOADPATH . $sub_path;
    $thumb_folder = GSTHUMBNAILPATH . $sub_path;
    $thumb_file = isset($out_file) && !empty($out_file) ? $thumb_folder . $out_file : '';
    create_dir($thumb_folder);
    require_once 'imagemanipulation.php';
    $objImage = new ImageManipulation($upload_folder . $file);
    if ($objImage->imageok) {
        if ($upscale) {
            $objImage->setUpscale();
        }
        // allow magnification
        if ($quality) {
            $objImage->setQuality($quality);
        }
        // set quality for jpg or png
        if (isset($output_format)) {
            $objImage->setOutputFormat($output_format);
        }
        // setoutput format, ignored if out_file specifies extension
        if (isset($w) && isset($h)) {
            $objImage->setImageWidth($w, $h);
        } elseif (isset($w)) {
            $objImage->setImageWidth($w);
            // if only specifiying width, scale to width only
            // $objImage->resize($w); // constrains both dimensions to $size, same as setImageWidth($w,$w);
        } elseif (isset($h)) {
            $objImage->setImageHeight($h);
            // if only specifiying width, scale to width only
        }
        if (isset($crop)) {
            $objImage->setAutoCrop($crop);
        }
        // die(print_r($objImage));
        $objImage->save($thumb_file, $show);
        return $objImage;
    } else {
        return false;
    }
}