-1

i have this code to upload files to a folder (uploads). In a file fileupload.php i have a input with the type file multiple. I the use th file (upload.php) that contains the code below, to upload files (images) to the folder (upload) and this works fine. But i also would like to create thumbnails to put in to (uploads/thumbs). And also i need to set the size for these thumbnails. Thank you.

My code so far:

<?php
ready = "OK, Uppladdat";
uploaddir = "images/" . $imagepath;
uploaddir_th = "images/thumbs/" . $imagepath;
allowed = array('jpg','jpeg','gif','png');
max_size = 5048 * 1024;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['size'] <= $max_size) 
{
$file_ext  = pathinfo($_FILES[$key]['name'],PATHINFO_EXTENSION);
$file_name = basename($_FILES[$key]['name'],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed)) 
{
$name = $_FILES[$key]['name'];
$x = 1;
while (file_exists($uploaddir.'/'.$name)) 
{
   $name = $file_name.'['.$x.'].'.$file_ext;
   $x++;
}
if (move_uploaded_file($_FILES[$key]['tmp_name'],$uploaddir.'/'.$name))
{
   chmod($uploaddir.'/'.$name, 0644);
}
else
{
die(error_get_last());
}
}
else
{
   die("Invalid file type");
}
}
else
{
  die("File size too big");
}

//thumbnail image making part

list($width, $height) = getimagesize($uploaddir.'/'.$name);
$modwidth = 200;
$modheight = 140;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($uploaddir.'/'.$name);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight);
/* echo "Thumbnail: <img src='images/thumbs".$imagepath."'>"; */
imagejpeg($image, $uploaddir_th.'/'.$name, 100);
}
echo $ready; 
?>
Patrik Idén
  • 355
  • 4
  • 14
  • And what is your solution thus far for your goal? Stackoverflow isn't a tutorial service or a code writing service. We are here to help with *code problems* - this isn't a *problem* - it's a request to write the functionality for you with no attempt of your own. – ProEvilz Oct 21 '18 at 12:02
  • OK, thank you. I have changed the code now in the first post. Now i get one image in to image folder and one image in to image/thumbs folder. But the problem is that the image that ends up in the thumbs folder is just as big in pixels as the main image, and it actualy gets bigger in kb size, the original image is about 78kb but the thumbnail image is about 100kb. So what is going on here. Thank you. – Patrik Idén Oct 24 '18 at 20:04

1 Answers1

1

You can use this PHP module and set the size of your thumbnails and also add different effects to your images http://php.net/manual/en/imagick.thumbnailimage.php

http://php.net/manual/en/book.imagick.php

itwolfpower
  • 306
  • 3
  • 11
  • Thank you, i have not tested the imagick module, but i'm getting there. I have changed the code now in the first post. Now i get one image in to image folder and one image in to image/thumbs folder. But the problem is that the image that ends up in the thumbs folder is just as big in pixels as the main image, and it actualy gets bigger in kb size, the original image is about 78kb but the thumbnail image is about 100kb. So what is going on here. Thank you. – Patrik Idén Oct 24 '18 at 20:05
  • Try this page it has a lot of methods also I Think you have some problems with quality and resampaling of the image(imagemagick processing methods) https://stackoverflow.com/questions/11376315/creating-a-thumbnail-from-an-uploaded-image – itwolfpower Oct 25 '18 at 13:21