phpのGD imagecopy の挙動についてご質問させていただきます。
目的としては
Aフォルダの中にある縦横比のランダムの画像を
Bフォルダに長いほうの辺サイズを基にコピーをする動作を構築中です。
問題になっている部分のざっくりとした流れとしては
imagecreatetruecolor
で$baseimages
として作成
2.imagefilledrectangle
を利用して白く塗りつぶし
3.imagecopy
で元画像を$baseimages
にコピー
4.imagejpeg
で出力
3で画像をコピーした後に元画像の短い辺のほうの背景塗りつぶし部分が黒く出力されてしまいます。
ソース
$arr = array(); //特定フォルダ中身のファイル取得 //テスト段階のため、ファイル種類のフィルタリングをしていない。 $dir = 'original/'; $handle = opendir($dir) or exit('NG'); while ($fname = readdir($handle)) { if(is_file($dir . $fname)){ array_push($arr, $fname); } } closedir($handle); //取り出したファイル一覧をループにかける $i = 0; $maxi = count($arr) - 1; while ($i <= $maxi) { $file = $dir.$arr[$i]; $image = imagecreatefromjpeg( $file ); //画像サイズ取得 $width = imagesx( $image ); $height = imagesy( $image ); //画像配置ポジション初期化 $wposition = 0; $hposition = 0; if($width > $height){ //横長画像の場合 $hposition = $height / 2; $hposition = ($width /2) - $hposition; $height = $width; }elseif($height > $width){ //縦長画像の場合 $wposition = $width / 2; $wposition = ($height /2) - $wposition; $width = $height; } //ベース作成 $baseimages = imagecreatetruecolor($width, $height ); //白く塗りつぶし $white = imagecolorallocate($baseimages, 33, 255, 255); imagefilledrectangle($baseimages, 0, 0, $width, $height, $white); //通常コピー imagecopy($baseimages,$image,$wposition,$hposition,0,0,$width,$height); imagedestroy($image); imagejpeg( $baseimages, 'copy/'.$arr[$i] ); $i++; }
photoshopなどのレイヤーのイメージで作成しているのですが、そもそもの考え方が違うのでしょうか。
画像コピー後足りない部分を再度塗りつぶし処理を加えて回避することは想像したのですが
スマートなやり方なのかわからなくなりご質問させていただきました。
記述方法などお見苦しい点もあるかとは思いますが、何卒ご教授いただけましたら幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/07 06:02