サムネイルを作ってみました。そして、php関数が動作します。それを使ってサムネイルを作りました。ただし、特定のファイルは機能しません。問題を絞り込みましたが、今は立ち往生しています。どんな助けでも大歓迎です。
画像はhttp://www.pspsigma.com/image_test.phpにあります。左側のものは動作しません。右の人がそうします。どちらも.jpgです。どちらも元々はフォーム経由でアップロードされていましたが、関数がフォームなしで機能するようにスタック オーバーフローのために変更されています。
html は、この .php ファイルへのファイル入力セット アクションを含む単純なスクリプトです。
PHP:
<?php
$the_name= $_FILES['userfile']['name'];
$tmpname = $_FILES['userfile']['tmp_name'];
$size="1000";
$save_dir="images/thumbs/";
$save_name=$the_name;
$maxisheight= "100";
function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight)
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = getimagesize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
//check if imorig is set
if (!isset($imorig)) {
echo " imorig is not set, ";
}
//I tried setting $x and $y from the width and height of the original file. Still didn't work.
//although the variables were set.
//this makes me think the problem is with the imagecreatefromjpeg function.
list($width, $height, $type, $attr) = getimagesize($tmpname);
echo "getimagesize width= " . $width . ", ";
echo "getimagesize height= " . $height . ", ";
//possible solution, didn't work
//$x = $width;
//$y = $height;
//This is the problem
$x = imagesx($imorig);
$y = imagesy($imorig);
//check that x and y are set
if (!isset($x) && !isset($y)) {
echo" x and y are not set,";
}
elseif (!isset($x)) {
echo" x is not set";
}
elseif (!isset($y)) {
echo" y is not set";
}
else {
echo "|x= " . $x . " |";
echo "|y= " . $y . " |";
}
$woh = (!$maxisheight)? $gis[0] : $gis[1] ;
if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight){
$aw = $size;
$ah = $size * $y / $x;
} else {
$aw = $size * $x / $y;
$ah = $size;
}
}
$im = imagecreatetruecolor($aw,$ah);
if (!$im) {
echo " failure: no im variable. ";
}
else {
if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y)) {
if (!imagejpeg($im, $save_dir.$save_name)) {
echo "failure";}
else {
echo "success";}
}
else {
echo" didn't copy resampled";
}
}
}
img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight);
?>
rush vader.jpg ファイルを選択して、この関数に通すとします。x と y が設定されていることをエコーし、それらの値と成功を表示します。
Calm.jpg を選択すると、「失敗: im 変数がありません」というメッセージが表示されます。$x および $y 変数には値がありません。x 変数と y 変数に getimagesize($tmp_name) の値を割り当てると、「リサンプリングされたものをコピーしませんでした」と表示されます。
これは実際の画像の問題ですか?または、すべての画像が機能するようにコードを変更する方法はありますか?
ありがとうございました!!!
アップデート
すべての変数をエコーして、何かがおかしいかどうかを確認しました。問題はタイプにあるようです。タイプは 6 として出力されますが、PHP のマニュアル コメントからわかったのは bmp です。
拡張子が .jpg の場合、それがどのようになるのかわかりません... これが当てはまる場合。タイプを.jpg、.png、または.gifのいずれかに変更する方法はありますか?