2013-11-09 31 views
0

我有软件,它通过POST将图像发送到服务器上的php-script。我必须裁剪每张​​照片。所以有2个步骤:用Curl和裁剪来获得它。但裁剪部分不起作用(卷曲部分效果不错,保存图像)。通过POST获取图像并裁剪它

`

// 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);` 
+0

你有你的服务器上安装PHP GD? –

+3

定义'不起作用'。 – 2013-11-09 19:58:57

+0

是的,安装GD。当我试图为1张图像运行“手动”时,剪切效果很好 –

回答

1

使用此功能可用于裁剪图像,它为我工作:

<?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']); 

?>