dongli8862 2013-11-09 19:52
浏览 25

通过POST获取图像并裁剪它

I have software, that 'sends' an images via POST to php-script on server. And i have to crop every this picture. So there 2 steps: get it with Curl and crop. But croping section doesn't work (Curl section works well and saving image).

`

// 1. GET IMAGES VIA POST
    $imgpath = $_POST['img_url']; 
    $dirname = $_POST['img_folder'];
    $imgid = $_POST['imgid'];
    $ch = curl_init($imgpath);
    $fn = ($imgid.substr($imgpath,strrpos($imgpath,'/')+1,strlen($imgpath)));
    $fn = str_replace('?', '_', $fn);
    $fn = str_replace('=', '_', $fn);
    if (is_dir($dirname)==FALSE) mkdir($dirname);
    $fp = fopen($dirname.'/'.$fn, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
// 2. CROP IMAGE
    $in_filename = $_POST['img_url'];
    list($width, $height) = getimagesize($in_filename);
    $offset_x = 0;
    $offset_y = 0;
    $new_height = $height - 40;
    $new_width = $width;
    $out_filename = $in_filename . '_crop';
    $image = imagecreatefromjpeg($in_filename);
    $new_image = imagecreatetruecolor($new_width, $new_height);
    imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
    header('Content-Type: image/jpeg');
    imagejpeg($new_image, $out_filename);
    imagedestroy($new_image);
    echo($dirname.'/'.$fn);`
  • 写回答

1条回答 默认 最新

  • douhezhan5348 2013-11-10 07:58
    关注

    Use this function for cropping images, it works for me:

    <?php
    
     header('Content-Type: image/jpeg');
     function cropPics($img, $x, $y, $width, $height) {
    
        $src = imagecreatefromjpeg($img);
        $dest = imagecreatetruecolor($width, $height);
        imagecopy($dest, $src, 0, 0, $x, $y, $width, $height);
        imagedestroy($src);
    
     cropPics('images/'.$_GET['i'], (int)$_GET['x'], (int)$_GET['y'], (int)$_GET['w'], (int)$_GET['h']);
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?