PHP使用imagick擴充實現合并映像的方法詳解

來源:互聯網
上載者:User
這篇文章主要介紹了PHP使用imagick擴充實現合并映像的方法,結合執行個體形式分析了php基於imagick擴充處理圖片的具體步驟與相關操作技巧,需要的朋友可以參考下


第一步:

// step1: 調整尺寸到 590 x 590$a = ROOT . '/' . 'a';// 掃描目錄$dirA = scandir($a);$im = new Imagick;foreach ($dirA as $item) {// 跳過目錄和縮圖if ($item === '.' || $item === '..' || strstr($item, '.db')) {continue;}// 讀取圖片$im->readImage($a . '/' . $item);// 擷取圖片寬x高$geo = $im->getImageGeometry();if ($geo['width'] === 590 && $geo['height'] === 590) {// 寬高符合,跳過} else {// 調整尺寸到590 x 590im->resizeImage(590, 590, Gmagick::FILTER_UNDEFINED, 1, TRUE);}// 將圖片儲存到另一目錄$im->writeImage(ROOT . '/_a/' . $item);// 釋放資源$im->destroy();}

第二步:

// step2: 合并圖片和名字// 掃描目錄$files = scandir(ROOT . '/_a');$k = 0;foreach ($files as $item) {// 跳過目錄和縮圖if ($item === '.' || $item === '..' || strstr($item, '.db')) {continue;}// 文本圖片的寬$twidth = 570;// 文本圖片的高$theight = 141;// 擷取圖片名$pathinfo = pathinfo($item);$filename = $pathinfo['filename'];// 初始化圖片對象$text = new Imagick;// 初始化繪製對象$draw = new ImagickDraw;// 設定字型,這裡是放到網站的font下的微軟雅黑$draw->setFont('font/msyh.ttf');// 文字大小$draw->setFontSize(40);// 文字顏色$draw->setFillColor(new ImagickPixel('#000000'));// 文字對齊$draw->setTextAlignment(Imagick::ALIGN_LEFT);// 擷取文字資訊,主要是長寬,因為要實現在圖片置中$a = $text->queryFontMetrics($draw, $filename);// 添加文字$draw->annotation(($twidth - $a['textWidth']) / 2, 80, $filename);// 建立映像$text->newImage($twidth, $theight, new ImagickPixel('#ffffff'));// 圖片格式$text->setImageFormat('png');// 繪製圖片$text->drawImage($draw);// 建立一個空白圖片用來做畫布$canvas = new Imagick;$canvas->newimage(570, 661, 'white');$canvas->setImageFormat('png');// 讀取圖片$pic = new Imagick;$pic->readImage(ROOT . '/_a/' . $item);$pic->scaleimage(470, 470, TRUE);// 將圖片合并到畫布$canvas->compositeImage($pic, Imagick::COMPOSITE_OVER, 50, 50);// 將文字合并到畫布$canvas->compositeimage($text, Imagick::COMPOSITE_OVER, 0, 520);// 儲存圖片到另一目錄$canvas->writeimage(ROOT . '/com_a/' . $item);$k++;echo "{$k} files proceeded.\n";}

第三步:

// step3: 合并每20張到一頁// 掃描目錄$files = scandir(ROOT . '/com_a');// 給圖片分組$i = $j = 0;$group = array();foreach ($files as $item) {  if ($item === '.' || $item === '..' || strstr($item, '.db')) {    continue;  }  $i++;  $group[$j][] = $item;  if ($i % 20 === 0) {    $j++;  }}$total = count($group);// 按組拼接圖片,A4紙尺寸,4x5的組合方式foreach ($group as $k => $v) {  $canvas = new Imagick;  $canvas->newimage(2480, 3508, 'white');  $canvas->setimageformat('png');  $i = $j = 0;  foreach ($v as $item) {    $im = new Imagick(ROOT . '/com_a/' . $item);    // 預留了150的左邊距    $x = 150 + $i * 570;    // 130的頂邊距    $y = 130 + $j * 661;    $canvas->compositeimage($im, Imagick::COMPOSITE_OVER, $x, $y);    // 每4張一行    if (($i + 1) % 4 === 0) {      $i = 0;      $j++;    } else {      $i++;    }  }  $canvas->writeimage(ROOT . '/merge_a/' . $k . '.png');  $c = $k + 1;  echo "Group {$c}/{$total} done.\n";}

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.