使用php裁剪图像

时间:2014-10-21 13:47:36

标签: php image crop

我尝试在上传图片时裁剪图像。到目前为止,我只设法调整它的大小,但如果图像是矩形,那么图像被压扁,看起来并不好看。我试图获得可用于我目前必须调整大小的函数的编码。我看到的那些我必须改变我的功能,我希望不要这样做。

这是我的功能

function createThumbnail($filename) {
     global $_SITE_FOLDER;
    //require 'config.php';
    $final_width_of_image = 82;
    $height = 85;
    $path_to_image_directory = $_SITE_FOLDER.'portfolio_images/';
    $path_to_thumbs_directory = $_SITE_FOLDER.'portfolio_images/thumbs/';

    if(preg_match('/[.](jpg)$/', $filename)) {
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);
    } else if (preg_match('/[.](gif)$/', $filename)) {
        $im = imagecreatefromgif($path_to_image_directory . $filename);
    } else if (preg_match('/[.](png)$/', $filename)) {
        $im = imagecreatefrompng($path_to_image_directory . $filename);
    }

    $ox = imagesx($im);
    $oy = imagesy($im);

    $nx = $final_width_of_image;
    $ny = floor($oy * ($final_width_of_image / $ox));
    //$ny = $height;

    $nm = imagecreatetruecolor($nx, $ny);

    imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);



    if(!file_exists($path_to_thumbs_directory)) {
      if(!mkdir($path_to_thumbs_directory)) {
           die("There was a problem. Please try again!");
      } 
       }

    imagejpeg($nm, $path_to_thumbs_directory . $filename);
    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
    $tn .= '<br />Congratulations. Your file has been successfully uploaded, and a      thumbnail has been created.';
    echo $tn;
}

2 个答案:

答案 0 :(得分:0)

这是我的功能。

 <?php
    function crop($file_input, $file_output, $crop = 'square',$percent = false) {
        list($w_i, $h_i, $type) = getimagesize($file_input);
        if (!$w_i || !$h_i) {
            echo 'Unable to get the length and width of the image';
            return;
            }
            $types = array('','gif','jpeg','png');
            $ext = $types[$type];
            if ($ext) {
                    $func = 'imagecreatefrom'.$ext;
                    $img = $func($file_input);
            } else {
                    echo 'Incorrect file format';
            return;
            }
        if ($crop == 'square') {
            $min = $w_i;
            if ($w_i > $h_i) $min = $h_i;
            $w_o = $h_o = $min;
        } else {
            list($x_o, $y_o, $w_o, $h_o) = $crop;
            if ($percent) {
                $w_o *= $w_i / 100;
                $h_o *= $h_i / 100;
                $x_o *= $w_i / 100;
                $y_o *= $h_i / 100;
            }
                    if ($w_o < 0) $w_o += $w_i;
                $w_o -= $x_o;
            if ($h_o < 0) $h_o += $h_i;
            $h_o -= $y_o;
        }
        $img_o = imagecreatetruecolor($w_o, $h_o);
        imagecopy($img_o, $img, 0, 0, $x_o, $y_o, $w_o, $h_o);
        if ($type == 2) {
            return imagejpeg($img_o,$file_output,100);
        } else {
            $func = 'image'.$ext;
            return $func($img_o,$file_output);
        }
    }
    ?>

你可以像这样打电话

crop($file_input, $file_output, $crop = 'square',$percent = false);

如果需要,还可以调整功能。

<?php
function resize($file_input, $file_output, $w_o, $h_o, $percent = false) {
    list($w_i, $h_i, $type) = getimagesize($file_input);
    if (!$w_i || !$h_i) {
        return;
        }
        $types = array('','gif','jpeg','png');
        $ext = $types[$type];
        if ($ext) {
                $func = 'imagecreatefrom'.$ext;
                $img = $func($file_input);
        } else {
        return;
        }
    if ($percent) {
        $w_o *= $w_i / 100;
        $h_o *= $h_i / 100;
    }
    if (!$h_o) $h_o = $w_o/($w_i/$h_i);
    if (!$w_o) $w_o = $h_o/($h_i/$w_i);

    $img_o = imagecreatetruecolor($w_o, $h_o);
    imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i);
    if ($type == 2) {
        return imagejpeg($img_o,$file_output,100);
    } else {
        $func = 'image'.$ext;
        return $func($img_o,$file_output);
    }
}
?>

resize($file_input, $file_output, $w_o, $h_o, $percent = false);

答案 1 :(得分:0)

您可以使用此处SimpleImage

中的SimpleImage类

[edit]此处包含更多功能的更新版本SimleImage Updated

我在将各种图像调整为较小的缩略图尺寸时使用此功能,同时保持宽高比和精确的图像尺寸输出。我用适合背景的颜色填充空白区域,这个类支持裁剪。探索cutFromCenter和maxareafill方法。

例如,在您的代码中,您根本不需要imagecreatefromjpeg();

包括课程include('SimpleImage.php');

然后;

$im = new SimpleImage();
$im->load($path_to_image_directory . $filename);
$im->maxareafill($output_width,$output_height, 0,0,0); // rgb
$im->save($path_to_image_directory . $filename);

我每天都在上课,发现它非常多才多艺。