1
私はPHPを使用して、.pngファイルのアップロードを処理しています。何らかの理由で私の最初の試みは黒と透明を交換していた。複数のスタックオーバーフローに関する質問を読んで解決策を試したところ、私は今、ファイルがアップロードされる段階にありますが、読めず、0バイトです。何がうまくいかないのですか? .JPEGがうまく働いている閲覧可能なファイルを作成しているPNGファイルを切り取ってアップロードする
(私は...他の質問から正確な答えを以下AM)、.PNGないので
コード私が使用しています:
$dest_image = ImageCreateTrueColor($target_width, $target_height);
switch ($search->found_extension()) {
case 'PNG':
case 'png':
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
$source_image = imagecreatefrompng($image);
$transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127);
imagefilledrectangle($dest_image, 0, 0, $target_width, $target_height, $transparent);
imagecopyresampled($dest_image, $source_image, 0, 0, $x, $y, $target_width, $target_height, $w, $h);
header('Content-type: image/png');
imagepng($dest_image, "../../" . $folder . "/" . cleanstring($userdata->id) . $hasher . ".png", $quality);
break;
case 'jpg':
case 'jpeg':
case 'JPG':
case 'JPEG':
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
$source_image = imagecreatefromjpeg($image);
imagecopyresampled($dest_image, $source_image, 0, 0, $x, $y, $target_width, $target_height, $w, $h);
header('Content-type: image/jpeg');
imagejpeg($dest_image, "../../" . $filename, $quality);
break;
}
しようとしたソリューションを:
How do I resize pngs with transparency in PHP?
Resize images with transparency in php
1.なぜ画像ヘッダを出力しているのですか?2、なぜpngとjpg画像に使用するパスに大きな違いがあるのですか?3. 'imagepng()'の戻り値は何ですか? – jeroen
@jeroenファイルを保存するときにイメージヘッダーが必須であると思ったのですが、パスの違いは拡張子が ".png/.jpg"だからです(動作したら書き換えます)。 'imagepng()'の戻り値を取得するにはどうすればよいですか?このコードはajax呼び出しによって呼び出されます。 – NealVDV
あなたはajax呼び出しでtext/html/jsonが戻ってくるので、画像ヘッダを設定しないでください。スクリプトからブラウザに直接画像を出力したいときにのみ行います。あなたのコードはかなり繰り返されているので、私はjpgとpngのために同じコードを使用するので、問題の場所を簡単に絞り込むことができます。 – jeroen