Instructions to compress images with TinyPNG Laravel PHP

Tram Ho

Hi everyone! I taught myself Laravel and came up with a pretty good idea to compress images directly in the folder when you use Ckfinder or TinyCME to upload photos Before I posted on the Free Tuts forum I asked something but the channel owner said this was difficult. the other, hate the attitude (slightly bad but if you say it, say it) M instructs you to do it in a simple way. Then get to work! First , you add and call the TinyPNG function to the project, find out on its page. Next you have to determine the folder where TinyCME uploads the images (you can set it to be in a folder you want) Then you call all the images in that folder with the PHP $files = File syntax: :files(public_path(‘/image/1/’)); Then you get the name of the image file with the array looping syntax

foreach ($files as $file) { $images[] = $file->getRelativePathname(); };

I’m here because I check and add it to the database, because the image is too much, but compressing it, the free part of TinyPNG will end, meaning if it’s already compressed, don’t compress it again to save compression.

The above statement means to count the number of images in the directory, and check if the image exists in the data? If yes, then ignore k compression, if not, compress and store that image in the database, Oh forget you create a table for it.

I tested it locally and it was ok, this gave me a headache a lot of time, because I didn’t have an idea to build. Hope it is of help to you. Thank you for reading, if there’s something you don’t understand, please comment.

Share the news now

Source : Viblo