OGX-3 | 妳木有小雞雞

死亡不是无知,而是不表态。

php5 用magickwand 固定大小製作縮略圖

with 2 comments

早期tc在做圖片上傳時,使用最早magickwand寫的縮略圖類,按照比例等比縮放。圖片不會變形,但查看圖片時會因為圖片的尺寸不一樣,窗口會忽大忽小改變。想從網上找一個現成的,發現所有的代碼幾乎都出自于一個地方,連註釋都沒有變,唯一變的就是作者,並且還是第一人稱。

花了點時間,自己寫了一個,放在這裡供參考!如果有問題歡迎指出。

  1. /**
  2. * 按照固定比率切圖
  3. *
  4. * @param string $r_src_img
  5. * @param string $r_dest_img
  6. * @param int $r_dest_width
  7. * @param int $r_dest_height
  8. * @param float $r_rate
  9. *
  10. * @return bool
  11. */
  12. public function zoomImageFixed($r_src_img,$r_dest_img,$r_dest_width = 480,$r_dest_height = 360,$r_rate = 0.8)
  13. {
  14.      // 對原始圖片壓縮50%
  15.      $_rate_img = self::cutThumbImage($r_src_img,$r_rate);
  16.  
  17.      if (MagickWriteImage($_rate_img,$r_dest_img)) {
  18.           // 計算裁切圖片的比例
  19.           $_dest_rate_WH = round($r_dest_width / $r_dest_height,3);
  20.  
  21.           // 原始圖片的寬高比
  22.           $_src_width = self::getImageWidth($r_dest_img);
  23.           $_src_height = self::getImageHeight($r_dest_img);
  24.           $_src_rate_WH = round($_src_width / $_src_height,3);
  25.  
  26.           if ($_src_width <= $r_dest_width && $_src_height <= $r_dest_height) {
  27.                 // 比縮略圖小的圖片不裁切
  28.                 $_height = $_src_height;
  29.                 $_width = $_src_width;
  30.                 $_thumb_height = $_src_height;
  31.                 $_thumb_width = $_src_width;
  32.                 $_position_X = ceil(abs($_src_width – $r_dest_width) / 2);
  33.                 $_position_Y = ceil(abs($_src_height – $r_dest_height) / 2);
  34.                 $_composite_position_X = $_position_X;
  35.                 $_composite_position_Y = $_position_Y;
  36.             } else if ($_src_rate_WH > $_dest_rate_WH) {
  37.                 // 按照高度比切圖片寬度
  38.                 if ($_src_height <= $r_dest_height) {
  39.                 $_height = $_src_height;
  40.                 $_width = $r_dest_width;
  41.  
  42.                 $_thumb_height = $_height;
  43.                 $_thumb_width = $_width;
  44.  
  45.                 $_position_X = ceil(abs($_src_width – $r_dest_width) / 2);
  46.                 $_position_Y = 0;
  47.  
  48.                 $_composite_position_X = 0;
  49.                 $_composite_position_Y = ceil(abs($_src_height – $r_dest_height) / 2);
  50.             } else {
  51.                 $_rate_h = round($r_dest_height / $_src_height,3);
  52.                 // 如果高度過小,則不拉伸圖片,以空白填充
  53.                 $_height = $r_dest_height;
  54.                 $_width = $r_dest_width;
  55.  
  56.                 // 縮圖
  57.                 $_thumb_height = $_height;
  58.                 $_thumb_width = ceil($_src_width * $_rate_h);
  59.  
  60.                 // 裁切開始位置
  61.                 $_position_X = ceil(abs($_thumb_width – $r_dest_width) / 2);
  62.                 $_position_Y = 0;
  63.  
  64.                 $_composite_position_X = 0;
  65.                 $_composite_position_Y = 0;
  66.             }
  67.         } else if ($_src_rate_WH < $_dest_rate_WH) {
  68.             // 按照寬度比切圖片高度
  69.             if ($_src_width <= $r_dest_width) {
  70.                 $_width = $_src_width;
  71.                 $_height = $r_dest_width;
  72.  
  73.                 // 縮圖的寬高
  74.                 $_thumb_width = $_width;
  75.                 $_thumb_height = $_height;
  76.                 // 縮圖時使用的座標
  77.                 $_position_X = 0;
  78.                 $_position_Y = ceil(abs($_src_height – $r_dest_height) / 2);
  79.  
  80.                 $_composite_position_X = ceil(abs($_src_width – $r_dest_width) / 2);
  81.                 $_composite_position_Y = 0;
  82.             } else {
  83.                 $_rate_w = round($r_dest_width / $_src_width,3);
  84.                 $_width = $r_dest_width;
  85.                 $_height = $r_dest_height;
  86.  
  87.                 // 縮圖
  88.                 $_thumb_width = $_width;
  89.                 $_thumb_height = ceil($_src_height * $_rate_w);
  90.  
  91.                 // 裁切開始位置
  92.                 $_position_X = 0;
  93.                 $_position_Y = ceil(abs($_thumb_height – $r_dest_height) / 2);
  94.  
  95.                 $_composite_position_X = 0;
  96.                 $_composite_position_Y = 0;
  97.             }
  98.         } else {
  99.             $_width = $r_dest_width;
  100.             $_height = $r_dest_height;
  101.             $_thumb_height = $_src_height;
  102.             $_thumb_width = $_src_width;
  103.             $_position_X = 0;
  104.             $_position_Y = 0;
  105.             $_composite_position_X = 0;
  106.             $_composite_position_Y = 0;
  107.         }
  108.  
  109.         // 開始切圖
  110.         $_obj_nmw = NewMagickWand();
  111.         // 讀取圖片
  112.         MagickReadImage($_obj_nmw,$r_dest_img);
  113.         $_obj_draw = NewDrawingWand();
  114.         $_obj_dest_nmw = NewMagickWand();
  115.         // 先把圖片按照比例縮小
  116.         MagickScaleImage($_obj_nmw,$_thumb_width,$_thumb_height);
  117.         // 合成一個新的圖片
  118.         DrawComposite($_obj_draw,MW_AddCompositeOp,$_composite_position_X,$_composite_position_Y,$_width,$_height,$_obj_nmw);
  119.         // 添加一個設定寬高背景色的新空白圖像
  120.         $_bg_color = ‘#FFFFFF’;
  121.         MagickNewImage($_obj_dest_nmw,$r_dest_width,$r_dest_height,$_bg_color);
  122.         // 上色
  123.         MagickDrawImage($_obj_dest_nmw,$_obj_draw);
  124.         MagickSetImageFormat($_obj_dest_nmw,MagickGetImageFormat($_obj_nmw));
  125.         MagickSetImageCompression($_obj_dest_nmw,MW_ZipCompression);
  126.         MagickSetImageCompressionQuality($_obj_dest_nmw,50);
  127.  
  128.         // 設置水印
  129.         if ($r_mark) {
  130.             $_font = ‘verdana.ttf’;
  131.             $_obj_pdw = NewPixelWand();
  132.             PixelSetColor($_obj_pdw,"black");
  133.             DrawSetFont($_obj_draw,$_font);
  134.             DrawSetFontSize($_obj_draw,12);
  135.             DrawSetFillColor($_obj_draw,$_obj_pdw);
  136.             DrawSetGravity($_obj_draw,MW_SouthGravity);
  137.             MagickAnnotateImage($_obj_dest_nmw,$_obj_draw,100,15,0,’xxxxxxxxx’);
  138.         }
  139.  
  140.         MagickWriteImage($_obj_dest_nmw,$r_dest_img);
  141.         return true;
  142.     }
  143.     return;
  144. }

先mark一下,然後等加水印后再繼續。

忘記附上結果了,貼代碼好費事。

—————————————————–苦惱的分割線啊————————

因為是在windows下開發,使用php5.3的版本,在使用magickwand成了最大的問題,從網上找的那些dll文件根本用不了。我都想自己編譯了,可是咱火候不到,都不知道dll做啥的,如果有熱心的同學可以幫幫我么?

msn:beiwei37.5@live.com

Written by beiwei

三月 4th, 2010 at 6:17 下午

2 Responses to 'php5 用magickwand 固定大小製作縮略圖'

Subscribe to comments with RSS

  1. You made fantastic nice points here. I performed a search on the issue and discovered almost all peoples will agree with your blog.

    Kristopher

    3 四 10 at 9:04 下午

  2. You gave honest ideas there. I did a search on the issue and got most peoples will agree with your blog.

    Alondra

    4 四 10 at 9:05 上午

Leave a Reply