I have simple HTML Form so you can upload mutliple images. I want to resize all of the images and set their width to 235. The code is working fine (resize image, and move it to the selected folder) without the foreach method. It's cool but my goal is to upload MULTIPLE images, resize them and finally move them into the selected folder. Any idea why it is not working with the foreach method?
<!DOCTYPE html>
<html>
<head>
<title>Image Resize</title>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input name='myImage' type='file' multiple />
<input name='submit' type="submit" />
</form>
</body>
</html>
upload.php:
<?php
if(isset($_POST["submit"])) {
if(is_array($_FILES)) {
print_r($_FILES);
foreach ($_FILES['myImage']['name'] as $key=>$val) {
$image_tmp = $_FILES["myImage"]["tmp_name"][$key];
$images_type = $_FILES["myImage"]["type"][$key];
$source_properties = getimagesize($image_tmp);
$image_type = $source_properties[2];
if( $image_type == IMAGETYPE_JPEG ) {
$image_resource_id = imagecreatefromjpeg($image_tmp);
$target_layer = fn_resize($image_resource_id, $source_properties[0], $source_properties[1]);
imagejpeg($target_layer, '/upload/img/'.substr(md5(rand()), 0, 7).''.date('YmdHis').'.'.basename($images_type));
}
elseif( $image_type == IMAGETYPE_GIF ) {
$image_resource_id = imagecreatefromgif($image_tmp);
$target_layer = fn_resize($image_resource_id, $source_properties[0], $source_properties[1]);
imagegif($target_layer, '/upload/img/'.substr(md5(rand()), 0, 7).''.date('YmdHis').'.'.basename($images_type));
}
elseif( $image_type == IMAGETYPE_PNG ) {
$image_resource_id = imagecreatefrompng($image_tmp);
$target_layer = fn_resize($image_resource_id, $source_properties[0], $source_properties[1]);
imagepng($target_layer, '/upload/img/'.substr(md5(rand()), 0, 7).''.date('YmdHis').'.'.basename($images_type));
}
}
}
}
function fn_resize($image_resource_id, $width, $height) {
$target_width = 235;
$target_height = 200;
$target_layer = imagecreatetruecolor($target_width, $target_height);
imagecopyresampled($target_layer, $image_resource_id, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
return $target_layer;
}
header('Location: index.html');
?>