【问题标题】:Saving Image with the actual scale gotten from image cropping tool使用从图像裁剪工具获得的实际比例保存图像
【发布时间】:2018-02-28 23:47:21
【问题描述】:

我正在尝试以与裁剪工具上相同的方式保存图像,使用我得到类似 X = 443Y = 180Width = 180height = 180 的处理,现在我的问题是如何发送它到 php 以调整原始图像的大小,如图像编辑器使用上述信息显示的那样。目前正在使用下面的 php 代码来调整图像的大小和高度,但添加 X 和 Y 是给我带来问题的原因。

<?php
function CroppedThumbnail($imagename, $path, $width, $height, $rename, $imgQuality, $ratio=false){
    $url = '';
    $info = pathinfo($imagename);
    $newpath = $path;
    $cdn_newpath = 'cdn/';
     //Rename the image
    if($rename == true){ $newFileName = $newpath . '/' . substr($info['filename'],0,5) . '-' . $width . 'x' . $height . '.jpeg';}
    else{ $newFileName = $newpath . '/' . $imagename;}

    $imageAvatar =  substr($info['filename'],0,5) . '-' . $width . 'x' . $height . '.jpeg';
    if(!file_exists($cdn_newpath.$newpath)){
        mkdir($cdn_newpath.$newpath, 0777, true);
        chmod($cdn_newpath.$newpath, 0755);
    }
    if(is_dir($cdn_newpath.$newpath . '/') && file_exists($cdn_newpath.$newFileName)){ return $url . $newFileName;}
    else{
   // Resample
    if ($info['extension'] == 'jpeg' OR $info['extension'] == 'jpg'){ $image = imagecreatefromjpeg($imagename);}
    else if ($info['extension'] == 'gif'){ $image = imagecreatefromgif($imagename);}
    else if ($info['extension'] == 'png'){ $image = imagecreatefrompng($imagename);}

    $widthx = imagesx( $image );
    $heighty = imagesy( $image );

    // calculate thumbnail size
    if($ratio == true){
       $new_width = $width;
       $new_height = floor( $heighty * ( $width / $widthx ) );
       $image_p = imagecreatetruecolor( $new_width, $new_height );
       imagecopyresampled( $image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $widthx, $heighty );
    }else{
      $image_p = imagecreatetruecolor($width, $height);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $widthx, $heighty);
    }
    imagejpeg($image_p, $cdn_newpath.$newFileName); 
    return $url . $newFileName;
    }
}
?>

【问题讨论】:

    标签: php image image-scaling


    【解决方案1】:

    你面临着和我一样的问题。请参阅下面我从课堂上提取的代码 sn-p。有些代码在你看来是伪代码。

    $trueFactorx = $original_height/$current_height;
    $trueFactory = $original_width/$current_width;
    
    // calls to my wrapper functions
    $img=image_crop($img,$crop_cordinates,$trueFactorx,$trueFactory);
    save_image($img,$image_type,$new_image_path);
    
    function image_crop($img1,$crop_cordinates,$factorX,$factorY)
    { 
        $corrdinate_array=explode(",", $crop_cordinates);
        $x=$corrdinate_array[0] * $factorX;
        $y=$corrdinate_array[1]* $factorY;
        $height=$corrdinate_array[2]* $factorY;
        $width=$corrdinate_array[3]* $factorX;
        $img2 = imagecreatetruecolor($width, $height);
        imagecopy($img2, $img1, 0, 0, $x, $y, $width, $height);
        return $img2;
    }
    
    
    function save_image($img,$image_type,$new_image_path)
    {
        switch(strtolower($image_type))
        {
            case 'png':
                header( 'Content-Type: image/png' );
                imagepng($img,$new_image_path);
            break;
            case 'gif':
                imagegif($img,$new_image_path);
            break;
        case 'jpeg':
            imagejpeg($img,$new_image_path);
            break;
        case 'jpg':
            imagejpeg($img,$new_image_path);
        break;
        default:
            die('Unsupported File!'); 
        }
    
    
        imagedestroy($img);
    
    }
    

    祝你好运!

    【讨论】:

    • 请您解释一下如何将我的 X、Y、宽度和高度传递给您的函数?
    • 感谢您尝试我的问题,实际上您的功能不起作用,但这一行修复了我自己的功能` imagecopy($img2, $img1, 0, 0, $x, $y, $width, $高度);`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多