By chance, I wrote the following script yesterday.
This is for resizing images, PNG or GIF or JPEG.
This requires './tmp' directory.
If you like, please refer to this.
<?php
$html = PHP_EOL;
if (!empty($_FILES['images'])) {
$finfo = new finfo(FILEINFO_MIME);
for ($i=0;;$i++) {
switch (true) {
case (!isset($_FILES['images']['tmp_name'][$i])):
break 2;
case (!is_uploaded_file($filename = $_FILES['images']['tmp_name'][$i])):
case (($type = $finfo->file($filename)) === false):
continue 2;
case ($type === 'image/png; charset=binary'):
$img = imagecreatefrompng($filename);
break;
case ($type === 'image/jpeg; charset=binary'):
$img = imagecreatefromjpeg($filename);
break;
case ($type === 'image/gif; charset=binary'):
$img = imagecreatefromgif($filename);
break;
default:
continue 2;
}
list($width, $height) = getimagesize($filename);
$new_width = 100;
$new_height = (int)($new_width * $height / $width);
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled(
$new_img, $img,
0, 0, 0, 0,
$new_width, $new_height, $width, $height
);
switch (true) {
case ($type === 'image/png; charset=binary'):
imagepng($new_img, $filename);
break;
case ($type === 'image/jpeg; charset=binary'):
imagejpeg($new_img, $filename);
break;
default:
imagegif($new_img, $filename);
}
$new_filename = './tmp/'.basename($filename);
if (move_uploaded_file($filename,$new_filename))
$html .= sprintf('<p><img src="%s" /></p>'.PHP_EOL, $new_filename);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Resizer</title>
<style>
label { display: block; }
</style>
</head>
<body>
<fieldset>
<legend>Select Image File (PNG, JPEG, GIF available)</legend>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<label><input type="file" name="images[]" /></label>
<label><input type="file" name="images[]" /></label>
<label><input type="file" name="images[]" /></label>
<label><input type="submit" value="Resize!" /></label>
</form>
</fieldset>
<fieldset>
<legend>Resized Images</legend><?php
echo $html;
?>
</fieldset>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…