私はphpnoobで、アップロードされた画像をより小さなサイズに縮小する画像サイズ変更スクリプトを作成しようとしています。以下の現在のスクリプトを使用すると、元の画像が表示されます。新しい幅と高さは登録されていないようです。どんな助けでも大歓迎です。
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
if (isset($_FILES['image']['tmp_name'])) {
if ($_FILES['image']['error']==0){
$new_dir = '/PHP_MySQL_Practice/uploaded/images';
$fullpath = $_SERVER['DOCUMENT_ROOT'].$new_dir;
if(!is_dir($fullpath)) {
mkdir($fullpath, 0777, TRUE);
}
//get file name and type(extension)
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
//separate image name after first "dot"
$separated = explode(".", $name);
//get string before the dot which was stored as the 1st item in the separated array.
$first = $separated[0];
//use php function pathinfo to get extension of image file
$ext = pathinfo($name, PATHINFO_EXTENSION);
//concatenate current timestamp (to avoid file overwrites) with the extracted file 'firstname' and add the extension
$name = time().'_'.$first.'.'.$ext;
$target = $fullpath.'/'.$name;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
resizeImage($target, $name);
echo '<img src="' . $target . '"/>';
}
}
else {
echo 'there was an error in saving ur image file.';
}
}
else {
echo 'ur image could not be uploaded.';
}
}
else {
?>
<form method="POST" enctype="multipart/form-data">
<label>upload image: </label>
<input type="file" name="image" />
<input type="submit" />
</form>
<?php
}
//function to resize image
function resizeImage($dir, $img) {
list($src_w, $src_h) = getimagesize($dir);
$max_w = 150;
$max_h = 150;
if ($src_w > $max_w) {
$ratio = $max_w / $src_w;
$new_img_w = $src_w * $ratio;
}
else if ($src_h > $max_h) {
$ratio = $max_h / $src_h;
$new_img_h = $src_h * $ratio;
}
$img_mime_type = getimagesize($dir);
switch ($img_mime_type['mime']) {
case 'image/jpeg':
case 'image/pjpeg':
$src = imagecreatefromjpeg($dir);
return $src;
break;
case 'image/gif':
$src = imagecreatefromgif($dir);
return $src;
break;
case 'image/png':
$src = imagecreatefrompng($dir);
return $src;
break;
default:
return FALSE;
break;
}
$new_img = imagecreatetruecolor($new_img_w, $new_img_h);
imagecopyresampled($new_img, $src, 0, 0, 0, 0, $new_img_w, $new_img_h, $max_w, $max_h);
switch ($img_mime_type['mime']) {
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($new_img, $dir, 100);
break;
case 'image/gif':
imagegif($new_img, $dir);
break;
case 'image/png':
imagepng($new_img, $dir);
break;
default:
return FALSE;
break;
}
imagedestroy($src);
imagedestroy($new_img);
}
?>