php图像裁剪服务器搭建-php教程

资源魔 79 0
正在咱们的工作的名目中,有时分咱们需求显示规则尺寸的图片,尽管能够经过css来管制显示巨细。然而假如图片过年夜,会造成加载的提早,影响网站全体功能。因而,咱们需求一个效劳器来协助咱们进行图片的裁剪。流程大抵是,起首咱们传给效劳器原图象以及裁剪的尺寸,而后效劳器进行裁剪,天生对应的裁剪图片,下次咱们再拜访相反图象以及相反的裁剪尺寸的时分,咱们就没有需求裁剪,间接进行图片的拜访就行。

Talk is cheap, show me the code.

<?php
// ①构建图片申请地点比方  http://xxx.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png
// ②设置装备摆设nginx重写规定  rewrite /s/(.*)/(\d+)x(\d+)-(\d)/(.*) /s/resize.php?site=$1&width=$2&height=$3&mode=$4&path=$5 last;
//③进行裁剪图片的解决
$path = trim($_GET['path']);
$mode = intval($_GET['mode']);
$site = trim($_GET['site']);
$width = intval($_GET['width']);
$height = intval($_GET['height']);
$site_list = array('crop' => '.');
$orig_dir = dirname(__FILE__);
if (!array_key_exists($site, $site_list)) {
    header('HTTP/1.1 400 Bad Request');
    exit();
}
if ($mode > 3 || $mode < 0) {
    header('HTTP/1.1 400 Bad Request');
    exit();
}
$orig_file = $site_list[$site] . $path;
if (!file_exists($orig_file)) {
    header('HTTP/1.1 404 Not Found');
    exit();
}
$file_ext = '.' . pathinfo($path, PATHINFO_EXTENSION);
$file_name = basename($path, $file_ext);
$save_path = "{$orig_dir}/{$site}/{$width}x{$height}-{$mode}{$path}";
$save_dir = dirname($save_path);
if (!file_exists($save_dir)) {
    wpx_mkdir($save_dir);
}
$target_width = $width;
$target_height = $height;
$save_image = $save_dir . '/' . $file_name . '.jpg';
if (file_exists($save_image)) {
    header('Content-Type: image/jpeg');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    echo file_get_contents($save_image);
}
imagecropper2($orig_file, $target_width, $target_height, $save_image);
die;
//原图象对应缩放裁剪,会拉伸图片
function imagecropper2($source_path, $width, $height, $save_image)
{
    
    //猎取原图象$filename的宽度$width_orig以及高度$height_orig
    $info =  getimagesize($source_path);
    
    $width_orig = $info[0];
    $height_orig = $info[1];
    $mime = $info['mime'];
    
    //依据参数$width以及$height值,换算出等比例缩放的高度以及宽度
    if ($width && ($width_orig<$height_orig)){
        $width = ($height/$height_orig)*$width_orig;
    }else{
        $height = ($width / $width_orig)*$height_orig;
    }
    //将原图缩放到这个新创立的图片资本中
    $image_p = imagecreatetruecolor($width, $height);
    //猎取原图的图象资本
    if($mime=='image/jpeg'){
        $image = imagecreatefromjpeg($source_path);
    }elseif($mime=='image/png'){
        $image = imagecreatefrompng($source_path);
    }elseif($mime=='image/gif'){
        $image = imagecreatefromgif($source_path);
    }
    
    //应用imagecopyresampled()函数进行缩放设置
    imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
   
    //将缩放后的图片$image_p保留,100(品质最好,文件最年夜)
    if($mime=='image/jpeg'){
        imagejpeg($image_p,$save_image);
        header('Content-Type: image/jpeg');
        imagejpeg($image_p);
    }elseif($mime=='image/png'){
        imagepng($image_p,$save_image);
        header('Content-Type: image/jpeg');
        imagepng($image_p);
    }else{
        imagegif($image_p,$save_image);
        header('Content-Type: image/jpeg');
        imagegif($image_p);
    }
    
}
//进行比例保留裁剪,会失落图象局部像素
function imagecropper($source_path, $target_width, $target_height, $save_image)
{
    $source_info = getimagesize($source_path);
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    $source_mime = $source_info['mime'];
    $source_ratio = $source_height / $source_width;
    $target_ratio = $target_height / $target_width;
    // 源图太高
    if ($source_ratio > $target_ratio) {
        $cropped_width = $source_width;
        $cropped_height = $source_width * $target_ratio;
        $source_x = 0;
        $source_y = ($source_height – $cropped_height) / 2;
    }
    // 源图过宽
    elseif ($source_ratio < $target_ratio) {
        $cropped_width = $source_height / $target_ratio;
        $cropped_height = $source_height;
        $source_x = ($source_width – $cropped_width) / 2;
        $source_y = 0;
    }
    // 源图适中
    else {
        $cropped_width = $source_width;
        $cropped_height = $source_height;
        $source_x = 0;
        $source_y = 0;
    }
    switch ($source_mime) {
        case 'image/gif':
            $source_image = imagecreatefromgif($source_path);
            break;
        case 'image/jpeg':
            $source_image = imagecreatefromjpeg($source_path);
            break;
        case 'image/png':
            $source_image = imagecreatefrompng($source_path);
            break;
        default:
            return false;
            break;
    }
    $target_image = imagecreatetruecolor($target_width, $target_height);
    $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
    // 裁剪
    $bool = imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
    // 缩放
    $bool = imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
    imagejpeg($target_image, $save_image);
    header('Content-Type: image/jpeg');
    imagejpeg($target_image);
    imagedestroy($source_image);
    imagedestroy($target_image);
    imagedestroy($cropped_image);
}
// 轮回天生目次
function wpx_mkdir($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) {
        return true;
    }
    if (!wpx_mkdir(dirname($dir), $mode)) {
        return false;
    }
    return @mkdir($dir, $mode);
}

经过下面的解决,咱们就将图片依照咱们设置的尺寸进行了裁剪。咱们还能够活期对裁剪图片进行清算,这样就没有需求占用太多效劳器空间。只有常常拜访的图片才会不断保留。

更多PHP相干常识,请拜访PHP中文网!

以上就是php图象裁剪效劳器搭建的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 图像 裁剪

抱歉,评论功能暂时关闭!