duanhuang4306 2017-08-03 13:12
浏览 62

PHP裁剪调整大小创建边框

Using the example in Croppic, I keep ending up with borders on one or more sides of an image when I crop some images. Example:

enter image description here

Here is the code I am using to process the image:

     // original sizes
    $imgInitW = $request->get('imgInitW');
    $imgInitH = $request->get('imgInitH');
     // resized sizes
    $imgW = $request->get('imgW');
    $imgH = $request->get('imgH');
     // offsets
    $imgY1 = $request->get('imgY1');
    $imgX1 = $request->get('imgX1');
     // crop box
    $cropW = $request->get('cropW');
    $cropH = $request->get('cropH');
     // rotation angle

     $output_filename = sys_get_temp_dir() . "/croppedImg_" . rand();

    $source_image = imagecreatefromjpeg($media->getPath());

    $type = '.jpeg';

    // resize the original image to size of editor
    $resizedImage = imagecreatetruecolor($imgW, $imgH);

    imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);

    // rotate the rezized image
    $rotated_image = imagerotate($resizedImage, -$angle, 0);
    // find new width & height of rotated image
    $rotated_width = imagesx($rotated_image);
    $rotated_height = imagesy($rotated_image);
    // diff between rotated & original sizes
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;
    // crop rotated image to fit into original rezized rectangle
    $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
    imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));

    imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
    // crop image into selected area
    $final_image = imagecreatetruecolor($cropW, $cropH);
    imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
    imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
    // finally output png image
    //imagepng($final_image, $output_filename.$type, $png_quality);
    imagejpeg($final_image, $output_filename . $type, '100');

This usually only happens when I reduce the image as far as possible in the croppic window.

I am at a loss and have tried adjusting every parameter I can find. Any ideas?

  • 写回答

1条回答 默认 最新

  • doudou3213 2017-08-03 13:32
    关注

    I actually found the answer here. I applied the forked version and it is working.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法