Codeigniter图像库使用2组坐标裁剪图像以在任何地方制作方形?

时间:2012-10-13 20:53:36

标签: php codeigniter image-processing gd

花时间阅读文档,并搜索示例。我理解从前0开始裁剪图像,左边0非常简单。然而。我想传递2组坐标,一个起点和一个终点。四点,一个在任何地方定义的广场。然而,从我发现的例子来看,从我收集的内容中,演绎不会让我这样做。 codeigniter所以我正在寻求对这个想法的确认,我是否真的只能提供0的终点,它根据所述终点裁剪一个正方形,或者我可以实际指定一个x开头,一个x结尾,以及类似的y ?

或者是否有一些我可以在codeigniter中使用的其他技术允许我传递起点和终点的坐标?

3 个答案:

答案 0 :(得分:1)

您可以使用已定义的裁剪图像类的示例,并通过codeigniter中的库或帮助程序将其称为类:

例如,请将其命名为:

LibraryCropImage::ResizedThumbnail($profile_thumbsize_x, $profile_thumbsize_x, $path, $filename, $path_to, $filename_to.'_sized', $quality, $fileextension);
LibraryCropImage::CroppedThumbnail($profile_thumbsize_x, $profile_thumbsize_x, $path, $filename, $path_to, $filename_to.'_cropped', $quality, $fileextension);

abstract class LibraryCropImage  {

    function ResizedThumbnail($thumbnail_width, $thumbnail_height, $path, $filename, $path_to, $filename_to, $quality, $ext)
    {
    list($width, $height) = getimagesize($path.$filename);

    $new_width = $thumbnail_width;
    $new_height = $thumbnail_height;

    if ($ext == "jpg" || $ext == "jpeg")
    {
        $thumb = @imagecreatefromjpeg($path.$filename);
    }
    else if ($ext == "gif")
    {
        $thumb = @imagecreatefromgif($path.$filename);
    }
    else if ($ext == "png")
    {
        $thumb = @imagecreatefrompng($path.$filename);
    }
    $thumbp = imagecreatetruecolor($new_width, $new_height);



    imagecopyresampled($thumbp, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    ob_start();
    if ($ext == "jpg" || $ext == "jpeg")
    {
        imagejpeg($thumbp, null, $quality);
    }
    else if ($ext == "gif")
    {
        imagegif($thumbp);
    }
    else if ($ext == "png")
    {
        imagepng($thumbp, null);
    }
    $i = ob_get_clean();
    $fp = fopen($path_to.$filename_to.'.'.$ext, 'w');
    fwrite ($fp, $i);
    fclose ($fp);
    imagedestroy($thumbp);
    }

    function CroppedThumbnail($thumbnail_width, $thumbnail_height, $path, $filename, $path_to, $filename_to, $quality, $ext)
    {
    list($width, $height) = getimagesize($path.$filename);

    $new_width = $thumbnail_width;
    $new_height = $thumbnail_height;

    if ($ext == "jpg" || $ext == "jpeg")
    {
        $image = @imagecreatefromjpeg($path.$filename);
    }
    else if ($ext == "gif")
    {
        $image = @imagecreatefromgif($path.$filename);
    }
    else if ($ext == "png")
    {
        $image = @imagecreatefrompng($path.$filename);
    }

    $filename = $path.$filename;

    $thumb_width = $thumbnail_width;
    $thumb_height = $thumbnail_height;

    $width = imagesx($image);
    $height = imagesy($image);

    $original_aspect = $width / $height;
    $thumb_aspect = $thumb_width / $thumb_height;

    if($original_aspect >= $thumb_aspect) {
       // If image is wider than thumbnail (in aspect ratio sense)
       $new_height = $thumb_height;
       $new_width = $width / ($height / $thumb_height);
    } else {
       // If the thumbnail is wider than the image
       $new_width = $thumb_width;
       $new_height = $height / ($width / $thumb_width);
    }

    $thumb = imagecreatetruecolor($thumb_width, $thumb_height);

    // Resize and crop 
    imagecopyresampled($thumb,
                       $image,
                       0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                       0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                       0, 0,
                       $new_width, $new_height,
                       $width, $height);


    ob_start();
    if ($ext == "jpg" || $ext == "jpeg")
    {
        imagejpeg($thumb, null, $quality);
    }
    else if ($ext == "gif")
    {
        imagegif($thumb);
    }
    else if ($ext == "png")
    {
        imagepng($thumb, null);
    }
    $i = ob_get_clean();
    $fp = fopen($path_to.$filename_to.'.'.$ext, 'w');
    fwrite ($fp, $i);
    fclose ($fp);
    imagedestroy($thumb);
    }
}

答案 1 :(得分:0)

给你的建议:

答案 2 :(得分:0)

我在这里回答了这个问题:how to crop image in codeigniter?

基本上,您提供两个轴(x / y)。但它不会像你想象的那样从0到该轴裁剪区域,它实际上将返回更接近中心的部分。因此,可以裁剪出(x1,y1)和(x2,y2)之间的区域。