0

花时间阅读文档并搜索示例。我了解从顶部 0 裁剪图像,左侧 0 非常简单。然而。我想传递两组坐标,一个起点和一个终点。四点,一个在任何地方定义的正方形。但是,从我找到的示例中,以及从我收集到的内容中,演绎不会让我这样做。codeigniter 所以我正在寻求对这个想法的确认,我是否只能提供从 0 开始的端点,并且它会根据所述端点裁剪一个正方形,或者我可以实际指定一个 x 开始,一个 x 结束,以及类似的 y ?

或者我可以在codeigniter中使用其他一些技术来传递起点和终点的坐标吗?

4

3 回答 3

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);
    }
}
于 2012-10-13T21:32:59.530 回答
0

给你的建议:

于 2012-10-13T23:05:37.240 回答
0

我在这里回答了这个问题:如何在 codeigniter 中裁剪图像?

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

于 2012-10-13T23:50:05.550 回答