2012-02-18 105 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); 
}