Jump to content

i have a simple html/php file setup.

Spoiler

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
Ikelk pora <input type="file" name="for_thumb"><br>
<input type="submit" name="submit" value="Kelt">

 

PHP part

Spoiler

$fullpath= "thumb/".$_FILES['for_thumb']['name'];
		@$mime=getimagesize($_FILES['for_thumb']['tmp_name']);
		if($mime['mime']=="image/jpeg")
		{	
			$dst_img= imagecreatetruecolor(150,84);
			$Size=getimagesize($_FILES['for_thumb']['tmp_name']);
			$src_img= imagecreatefromjpeg($_FILES['for_thumb']['tmp_name']);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,150,84,$Size[0],$Size[1]);
			imagejpeg($dst_img, $fullpath, 100);
			}
		else
			echo" Not JPG";
        }
        foreach(glob("thumb/*") as $file)// this part needs fixing
            echo "<img src='$file'>  ";

 

So long story short uploaded image gets resized and then all images in the folder thumb are printed. Though the printing part is good for testing but isn't efficient in real world applications. How should i replace my sluggish code?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/637516-create-thumbnails-from-uploaded-images-php/
Share on other sites

Link to post
Share on other sites

You shouldn't use any information that user could have submitted without escaping or making safe.

In your case, it's not smart  to use put $_FILES['for_thumb']['name'] in the $fullpath variable, because an "evil" user may send a specially crafted server request to change the name to some binary data or whatever they want. Also, users may use the same name for several files so you may overwrite older images or thumbs this way.

My first choice would be to generate a MD5 or a SHA256 of the contents of the image and then save the hash in the database or whatever you use, then save the thumbnail with a path something like this  /thumbs/a/a4/a4633f4e.....jpg  where the "a" and "a4" folders are simply formed from the first two characters in the hash.

This is optional, but the reasoning for this is because some file systems don't "like", they're slower when there's more than a few thousand files stored into a single folder. By making a folder structure like that, there's gonna be a few images in each folder branch, instead of thousands of thumbs in just one folder.

 

As for the foreach, I don't see the need for it.. aren't you creating the thumbnails a few lines of code above? Why not put those paths in an array as the pictures are created and read the array back at that later point? This way there's no need to parse the folder where the thumbs are created.

 

Also, you should be careful about imagecreatefromjpeg , because it's kind of memory hungry. Don't know how your code is going to use so i'm throwing this situation out there: some people may upload pictures made with their phone cameras, you may have 8-16 megapixel pictures (like, 5000 x 4000) and these pictures when they're decoded to raw images they may use up to 15-30 MB of memory. Some php installations don't allow so much memory usage per script and may crash or the image may fail to load - you may want to at least use the @  character in front of the functions to prevent errors and warnings from being dumped on the output and to test the return of those functions.

 

On a image sharing site I wrote in an afternoon (something super basic), I just resorted to installing imagemagick on the server and calling that to convert images instead of using the built in functions.

Link to post
Share on other sites

3 hours ago, mariushm said:

I just resorted to installing imagemagick on the server

I use the same utility for my application - it's excellent.

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to post
Share on other sites

13 hours ago, mariushm said:

As for the foreach, I don't see the need for it.. aren't you creating the thumbnails a few lines of code above? Why not put those paths in an array as the pictures are created and read the array back at that later point? This way there's no need to parse the folder where the thumbs are created.

My idea:

Select image, hit upload, see the first thumbnail. Select again (if you will), hit upload and now see two thumbnails and so on.

Since the page refreshes itself after pressing the upload button i cannot create an array of paths (at least to my knowledge) because it will keep resetting the array.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

2 hours ago, MisterWhite said:

My idea:

Select image, hit upload, see the first thumbnail. Select again (if you will), hit upload and now see two thumbnails and so on.

Since the page refreshes itself after pressing the upload button i cannot create an array of paths (at least to my knowledge) because it will keep resetting the array.

This is where you use AJAX to change part of the page without reloading. jQuery has shorthand notations to make doing AJAX easier.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×