上传时,我需要调整任何图像的大小.需要宽度为500px、高度为700px的输出.有人知道吗



当我使用下面的代码时。我得到了很多不同的裁剪图像,需要500像素宽和700像素高的输出。有人知道吗?代码:

<?php
function img_resize($ini_path, $dest_path, $params = array()) {
    $width = !empty($params['width']) ? $params['width'] : null;
    $height = !empty($params['height']) ? $params['height'] : null;
    $constraint = !empty($params['constraint']) ? $params['constraint'] : false;
    $rgb = !empty($params['rgb']) ?  $params['rgb'] : 0xFFFFFF;
    $quality = !empty($params['quality']) ?  $params['quality'] : 100;
    $aspect_ratio = isset($params['aspect_ratio']) ?  $params['aspect_ratio'] : true;
    $crop = isset($params['crop']) ?  $params['crop'] : true;
    if (!file_exists($ini_path)) return false;

    if (!is_dir($dir=dirname($dest_path))) mkdir($dir);
    $img_info = getimagesize($ini_path);
    if ($img_info === false) return false;
    $ini_p = $img_info[0]/$img_info[1];
    if ( $constraint ) {
        $con_p = $constraint['width']/$constraint['height'];
        $calc_p = $constraint['width']/$img_info[0];
        if ( $ini_p < $con_p ) {
            $height = $constraint['height'];
            $width = $height*$ini_p;
        } else {
            $width = $constraint['width'];
            $height = $img_info[1]*$calc_p;
        }
    } else {
        if ( !$width && $height ) {
            $width = ($height*$img_info[0])/$img_info[1];
        } else if ( !$height && $width ) {
            $height = ($width*$img_info[1])/$img_info[0];
        } else if ( !$height && !$width ) {
            $width = $img_info[0];
            $height = $img_info[1];
        }
    }
    preg_match('/.([^.]+)$/i',basename($dest_path), $match);
    $ext = $match[1];
    $output_format = ($ext == 'jpg') ? 'jpeg' : $ext;
    $format = strtolower(substr($img_info['mime'], strpos($img_info['mime'], '/')+1));
    $icfunc = "imagecreatefrom" . $format;
    $iresfunc = "image" . $output_format;
    if (!function_exists($icfunc)) return false;
    $dst_x = $dst_y = 0;
    $src_x = $src_y = 0;
    $res_p = $height/$width;
    if ( $crop && !$constraint ) {
        $dst_w  = $height;
        $dst_h = $width;
        if ( $ini_p > $res_p ) {
            $src_h = $img_info[1];
            $src_w = $img_info[1]*$res_p;
            $src_x = ($img_info[0] >= $src_w) ? floor(($img_info[0] - $src_w) / 2) : $src_w;
        } else {
            $src_w = $img_info[0];
            $src_h = $img_info[0]/$res_p;
            $src_y    = ($img_info[1] >= $src_h) ? floor(($img_info[1] - $src_h) / 2) : $src_h;
        }
    } else {
        if ( $ini_p > $res_p ) {
            $dst_w = $height;
            $dst_h = $aspect_ratio ? floor($dst_w/$img_info[0]*$img_info[1]) : $width;
            $dst_y = $aspect_ratio ? floor(($width-$dst_h)/2) : 0;
        } else {
            $dst_h = $width;
            $dst_w = $aspect_ratio ? floor($dst_h/$img_info[1]*$img_info[0]) : $height;
            $dst_x = $aspect_ratio ? floor(($height-$dst_w)/2) : 0;
        }
        $src_w = $img_info[0];
        $src_h = $img_info[1];
    }
    $isrc = $icfunc($ini_path);
    $idest = imagecreatetruecolor($height,$width     );
    if ( ($format == 'png' || $format == 'gif') && $output_format == $format ) {
        imagealphablending($idest, false);
        imagesavealpha($idest,true);
        imagefill($idest, 0, 0, IMG_COLOR_TRANSPARENT);
        imagealphablending($isrc, true);
        $quality = 0;
    } else {
        imagefill($idest, 0, 0, $rgb);
    }
    imagecopyresampled($idest, $isrc, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    $res = $iresfunc($idest, $dest_path, $quality);
    imagedestroy($isrc);
    imagedestroy($idest);
    return $res;
}
$params = array(
    'constraint' => array('width' => 500, 'height' => 700)
);
img_resize('imagesblurred1.jpg', 'imageskoalaresize.jpg', $params);
?>

只需使用下面的代码并传递一些重要参数,它就会返回一个具有所需宽度和高度的图像。

compressImage('jpg', 'Penguins.jpg', 'Penguins.jpg', 'Penguins.jpg', 500, 700);
function compressImage($ext,$uploadedfile,$path,$actual_image_name,$newwidth, $newheight)
{
    if($ext=="jpg" || $ext=="jpeg" ) $src = imagecreatefromjpeg($uploadedfile);
    else if($ext=="png") $src = imagecreatefrompng($uploadedfile);
    else if($ext=="gif") $src = imagecreatefromgif($uploadedfile);
    else $src = imagecreatefrombmp($uploadedfile);
    list($width,$height)=getimagesize($uploadedfile);
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $filename = $path.$newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
    imagejpeg($tmp,$filename,100);
    imagedestroy($tmp);
    return $filename;
}

最新更新