Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

compressing images after upload

I usually use this site to compress my images before using them:

https://tinypng.com/

I see they have an API so I would rather just let it handle the compression and upload in my actual from when I add a product. it would save  having to go to the website, upload it, download it and then upload it again on my site. I can't seem to find much detail in the documentation as how to implement thought. Not sure if anyone has experience with this?

https://tinypng.com/developers/reference/php

I was using this W3schools script but want to replace it with the API

	$target_dir = "uploads/";
			$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
			$uploadOk = 1;
			$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
			// Check if image file is a actual image or fake image
			$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    		if($check !== false) {
        	echo "File is an image - " . $check["mime"] . ".";
        	$uploadOk = 1;
    		} else {
        	echo "File is not an image.";
				$uploadOk = 0;
    }
			

			// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crazy Horse

ASKER

In my particular instance, it isn't about saving space as so much getting a better GTmetrix score as you get lower rankings if your images aren't compressed.

https://gtmetrix.com/

As for your suggestion in point number 2, that is a great idea and I think I managed to find something that does something similar.

https://github.com/ImageOptim/php-imageoptim-api#readme
getting a better GTmetrix score ...
I don't know how a GTMetrix score might translate into something of value (like money) but there's probably no harm in following their guidance.  For the best possible web site visibility, buy Google Adwords!  But that said, if it's not too much work to optimize the images, it's probably useful, especially if you expect your clients to be using low-speed internet connections.  You may also want to consider server-side cache - it will boost the overall performance of your web site.  And, of course, do all the right and standard things to make your database lightning fast.
Well, it could translate into money if you pitch it right ;) For example, you do some research and find that in a particular city there are a websites for a certain type of business but there is one without a website. You approach them and show them their competitors websites and that they have a low score. You then show them the mock-up you did for their site which you optimized properly by caching, compressing images, asynchronously loading scripts, minifying your css/html/js, etc. and all of a sudden their face lights up when they see their competitors with a score of 48/100 and their site is so old it isn't even built on bootstrap and isn't at all responsive. They they see yours with a score of 92/100 and fully responsive.

Anyway, I'm probably talking nonsense.

I will check out your article on caching. I just use a htaccess file for that currently.

I found something else that I have almost got working which is awesome. In case anyone is interested, here it is:

https://im2.io/api