私はうまく機能するアップロードスクリプトを使用しています:
// Create image from file
switch(strtolower($_FILES['fileField']['type'])) {
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['fileField']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['fileField']['tmp_name']);
break;
default:
exit('Unsupported type: '.$_FILES['fileField']['type']);
}
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Target dimensions for large version
$max_width = '600';
if($max_width > $old_width) {
$max_width = $old_width;
}
$max_height = ($old_height/$old_width)* $max_width;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
//Output the image to a file
imagejpeg($new, $folder.$newFileName,100);
// Destroy resources
imagedestroy($image);
imagedestroy($new);
このスクリプトを関数内に配置して、毎回異なる $max_width 値を使用して複数回実行し、そのコードを何度も複製することなく、同じ画像の複数のコピーを異なるサイズで作成できるようにします。
これが私の試みです:
// Create image from file
switch(strtolower($_FILES['fileField']['type'])) {
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['fileField']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['fileField']['tmp_name']);
break;
default:
exit('Unsupported type: '.$_FILES['fileField']['type']);
}
function resizeAndPlaceFile($target_max_width) {
global $image;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Target dimensions for large version
$max_width = $target_max_width;
if($max_width > $old_width) {
$max_width = $old_width;
}
$max_height = ($old_height/$old_width)* $max_width;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
//Output the image to a file
imagejpeg($new, $folder.$newFileName,100);
// Destroy resources
imagedestroy($image);
imagedestroy($new);
}
resizeAndPlaceFile('600');
ご覧のとおり、コードを関数内に配置し、グローバル $image 変数を追加し、ハードコードされた $max_width 値を $target_max_width と等しくなるように変更しました。必要なパラメータ。
アップロード成功のメッセージが表示される代わりに、画面全体に次の記号が表示されます。
PHP を使用して関数内に配置すると、アップロード スクリプトが機能しないのはなぜですか?