【问题标题】:Image crop/resize returns more of the image then selected. PHP图像裁剪/调整大小返回更多的图像然后选择。 PHP
【发布时间】:2018-03-15 14:04:42
【问题描述】:

我正在创建一个图像大小调整工具。选择等是使用 JavaScript 完成的,然后将值发送到 PHP。那工作得很好。但是,图像未正确调整大小。新图像获得正确的宽度/高度,但图像的选定部分被“缩放”,并且与选择的图像相比,图像的更多部分被返回。下图中的示例。

调整大小前的图像宽度/高度

宽度:280px 高度:210px

调整大小细节

输出图像

<?php

    $width = 163.094;
    $height = 148.5;
    $new_width = 62.18745;
    $new_height = 69.87505;

    $dst_left = 0;
    $dst_top = 0;
    $src_left = 49.15625;
    $src_top = 39.03125;

    header('Content-Type: image/png');

    // Create a empty image
    $new_image = imagecreatetruecolor($new_width, $new_height);

    // Set background transparent
    imagefill($new_image,0,0,0x7fff0000);

    // Create image
    $image = imagecreatefrompng("../../uploads/img/imageNX4jDQ.png");

    // Create new image
    imagecopyresampled($new_image, $image, $dst_left, $dst_top, $src_left, $src_top, $new_width, $new_height, $width, $height);

    // Keep transparency
    imageAlphaBlending($new_image, true);
    imageSaveAlpha($new_image, true);

    // Output
    $test = imagepng($new_image, "../../uploads/img/test.png");

    $test;

?>

【问题讨论】:

  • 看看你对 imagecopyresampled() 的论证
  • 我找不到他们有什么问题?我做错了什么?

标签: php


【解决方案1】:

这张图总是对我有帮助:

http://php.net/manual/en/function.imagecopyresampled.php#112742

除此之外,我预计您的 JS 会通过 62.18745 的原始宽度和 69.87505 的高度。

如果之后您需要缩小图像,那么您的 new_width 和 height 值必须根据您的需要进行调整。

【讨论】:

  • 我误解了 imagecopyresampled() 函数。我意识到我必须先用原始尺寸调整图像的大小。然后复制我想要的部分并从中生成一个新图像。
  • 哦,看,我先在评论中回答,没有得分。何必呢?
猜你喜欢
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多