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

Php Codeigniter图像库使用2组坐标裁剪图像以在任何地方形成正方形?,php,codeigniter,image-processing,gd,Php,Codeigniter,Image Processing,Gd,花时间阅读文档,并搜索示例。我理解从顶部0裁剪图像,而左侧0非常直接。然而。我想传递两组坐标,一个起点和一个终点。四个点,一个在任何地方定义的正方形。然而,从我发现的例子来看,从我收集的资料来看,我不会让我这样做。codeigniter所以我正在寻求对这个想法的确认,是真的我只能提供从0开始的端点,它根据所说的端点裁剪出一个正方形,还是我可以为y指定一个x开始,一个x结束,以及类似的 或者,我可以在codeigniter中使用其他技术来传递起点和终点的坐标吗?您可以使用定义的裁剪图像类作为示例,

花时间阅读文档,并搜索示例。我理解从顶部0裁剪图像,而左侧0非常直接。然而。我想传递两组坐标,一个起点和一个终点。四个点,一个在任何地方定义的正方形。然而,从我发现的例子来看,从我收集的资料来看,我不会让我这样做。codeigniter所以我正在寻求对这个想法的确认,是真的我只能提供从0开始的端点,它根据所说的端点裁剪出一个正方形,还是我可以为y指定一个x开始,一个x结束,以及类似的


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

您可以使用定义的裁剪图像类作为示例,并在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);
    }
}
给你的建议:
  • 使用
  • 与on Debian结合使用
  • 使用或结合使用

    • 我在这里回答了这个问题:

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