2012-02-18 216 views
0

我想创建缩略图,同时调整图像,所以在这里多待明确的是,我试图裁剪图像:创建缩略图和调整问题

enter image description here

而且我想切出那个红色区域。

现在我的问题是,我调整我的HTML图像croping所以之前,当我将数据提交到PHP时得到不正确的值,如y = 100时候真的也可能是y = 200所以我需要找到一种方法来计算自己的价值观。

我正在使用imagecopyresampled,也许有更好的命令呢?

而且我最亲密的soliution是这样的:

imagecopyresampled(
    $thumb, //Destination image link resource. 
    $src, //Source image link resource. 
    0,  //x-coordinate of destination point. 
    0,  //y-coordinate of destination point. 
    0,  //x-coordinate of source point. 
    0,  //y-coordinate of source point. 
    120, //Destination width. 
    160, //Destination height. 
    $image_width/2, //Source width. 
    $image_height/2 //Source height. 
); 

在这种情况下,将切出左侧角球,但尺寸会不一样,我的红盒子。 所以我想我需要得到source widthsource height权和其他一切应该伏贴,反正我希望我在这里做任何意义:)

编辑对不起,我忘了提,$image_width$image_height是原始图像大小

EDIT 2更清楚,这是我所得到的,当我使用此代码

$dimensions = getimagesize('testas.jpg'); 

$img = imagecreatetruecolor(120, 160); 
$src = imagecreatefromjpeg('testas.jpg'); 

imagecopyresampled($img, $src, 0, 0, 0, 0, 120, 160, $dimensions[0]/2, $dimensions[1]/2); 

imagejpeg($img, 'test.jpg'); 

enter image description here调整

调整后的图像大小是正确的,但正如你所看到的,它看起来不正确。

+0

您使用哪些命令获取不正确的值?如果按照BenM的建议在上传后使用'getimagesize',那应该没问题。 – 2012-02-18 23:19:24

回答

1

我使用这样的缩放图像:

public static function scaleProportional($img_w,$img_h,$max=50) 
{ 
    $w = 0; 
    $h = 0; 

    $img_w > $img_h ? $w = $img_w/$img_h : $w = 1; 
    $img_h > $img_w ? $h = $img_h/$img_w : $h = 1; 

    $ws = $w > $h ? $ws = ($w/$w) * $max : $ws = (1/$h) * $max; 
    $hs = $h > $w ? $hs = ($h/$h) * $max : $hs = (1/$w) * $max; 

    return array(
     'width'=>$ws, 
     'height'=>$hs 
    ); 
} 

用法:

  $getScale = Common::scaleProportional($prevWidth,$prevHeight,$scaleArray[$i][1]); 
      $targ_w = $getScale['width']; 
      $targ_h = $getScale['height']; 
      $jpeg_quality = 100; 

      $src = $prevdest; 

      $img_r = imagecreatefromjpeg($src); 
      $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 
      //imagecopyresampled(dest_img,src_img,dst_x,dst_y,src_x,src_y,dst_w,dst_h,src_w,src_h); 
      imagecopyresampled($dst_r,$img_r,0,0,0,0,$targ_w,$targ_h,$prevWidth,$prevHeight); 
      imagejpeg($dst_r, 'assets/images/'.$scaleArray[$i][0].'/'.$filename, $jpeg_quality); 
      $complete[] = $scaleArray[$i][0]; 
0

当您说'使用HTML调整大小'时,是指使用img元素的widthheight属性指定大小?如果是这样,这不会影响服务器上文件的尺寸,并且仍然可以使用getimagesize

像这样将返回图像的源宽度和高度:

function get_source_size($the_file_path) 
{ 
    $imagesize = getimagesize($the_file_path); 
    return array('width' => $imagesize[0], 'height' => $imagesize[1]); 
} 
+0

对不起,我忘了提及它,请查看我的更新 – Linas 2012-02-18 23:11:07

0

所以有的时候我能和我的朋友的帮助做到这一点后,这是我使用的脚本,有许多人将需要在未来:)

private function crop($user, $post){ 
    //get original image size 
    $dimensions = getimagesize($post['image_src']); 

    //get crop box dimensions 
    $width = $post['w'] * ($dimensions[0]/$post['img_width']); 
    $height = $post['h'] * ($dimensions[1]/$post['img_height']); 

    //get crop box offset 
    $y = $post['y'] * ($dimensions[1]/$post['img_height']); 
    $x = $post['x'] * ($dimensions[0]/$post['img_width']); 

    //create image 120x160 
    $img = imagecreatetruecolor(120, 160); 
    $src = imagecreatefromjpeg($post['image_src']); 

    imagecopyresampled($img, $src, 0, 0, $x, $y, 120, 160, $width, $height); 

    //and save the image 
    return imagejpeg($img, 'uploads/avatars/'.$user->id.'/small/'.$post['image_name'].".jpg" , 100); 
}