◯ CakePHP3 ImageMagickで写真(画像)リサイズするサンプルコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // サンプルコード // 縦: xxxpx -> 600px // 写真パス $photo_file_path = '写真パス'; // 写真読み込み $imagick_img = new \Imagick($photo_file_path); // 写真サイズ取得 $img_width = $imagick_img->getImageWidth(); $img_height = $imagick_img->getImageHeight(); // 写真リサイズ // 縦サイズ601px以上だったら縦サイズ600pxにリサイズ $max_height = 600; if (!empty($img_width) && !empty($img_height) && ($max_height < $img_height)) { // リサイズ処理 if (!$imagick_img->resizeImage(0, $max_height, \Imagick::FILTER_POINT, 1)) { // resizeImageエラー } } // 写真上書き保存 if (!$imagick_img->writeImage($photo_file_path)) { // 上書き保存エラー } // Imagick clear $imagick_img->clear(); |