1 Star2 Stars3 Stars4 Stars5 Stars (23 votes, average: 5.00 out of 5)

Optimize Images With PHP In A Directory on Your Server

Optimizing Images With PHP *

Optimizing images is a big part of website development and optimization. For most webmasters I do not have to go into detail on why you should optimize your images. For those of you who are not programmers please see information below.

For programmers and php program enthusiast I created a tutorial that will help programmers in this process.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂

You can copy and paste the code or download it here. optimize images in php

Subscribe To My YouTube Channel:

Get Code Here

Increase Memory *

It may be necessary to increase your memory limit in your php.ini file while performing this process. If you want to view your current memory limit download this file and upload it to your server. PHPINFO This will give you read out information about memory limits.

php memory limityou can simply make this file yourself in php and upload it to do the same thing. Here is the code.

<?php phpinfo(); ?>

Here is the php.ini file to download so you can increase your memory size. You want to upload this file to your main directory. php.ini

Examples Before and After *

Before

Optimize Images in PHP

AfterOptimize Images in PHP

Optimize Images For Non Techie People *

If you are a non techie person and want to use a plugin for optimizing your images there are a couple. I will include them here.

 

 

 

Optimize Images With PHP In A Directory on Your Server was last modified: November 26th, 2018 by Maximus Mccullough

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Cool People Share:
Optimize Images With PHP In A Directory on Your Server

13 Comments

  • Cat Nduanya says:

    Great article, Maximus! Worked like a charm!

  • Bart says:

    What if I don’t want to resize images?

  • ravi says:

    hi,
    can you tell us where to place the above code and what other changes has to be made? for beginners it is not quite clear.

    many thanks

    • Hi Ravi,
      You want to place this script in your public_html or if you are on a Linux system in the web folder. Make SURE you back everything up before you do this procedure. What this script will do is go into your uploads folder and resize all the images in it. So you have to make sure that your paths are correct. For example your uploads folder might me upload or pics etc. After you have everything backed up you want to run this script by loading it in your browser. It may take some time depending on how many pictures your have in your folder.

  • Boris says:

    Hi
    You help me a lot. Thank you very much.

  • ravioli says:

    Is there a way to check if image was optimized so it doesn’t optimize it again?

  • Ian Haney says:

    I was just looking at using this script but want to keep the optimised images in the same directory on the server, is that possible to do? as thinking if the optimised images are moved into a new folder I will need to update the code where the images are on each page or can I just have the images and optimised images directories the same name? Also does it keep the transparency on png images as know some php image optimisation scripts make the png images have a black background

    • I am going to respond to you email inline.

      I was just looking at using this script but want to keep the optimised images in the same directory on the server, is that possible to do? Yes it is. as thinking if the optimised images are moved into a new folder I will need to update the code where the images are on each page or can I just have the images and optimised images directories the same name? Yes you can do that. Also does it keep the transparency on png images as know some php image optimisation scripts make the png images have a black background. No it does not keep the transparency. PNG images should not be used on the web anyway according to Google. I do it and I probably shouldn’t but I will upgrade later, lol

  • Ian Haney says:

    Hi

    Thank you for replying, really appreciate it. Is it best to email you or comment back on here?

    I need the png images on the site as they have a transparent background, if I was to use your script to optimise the images, would the following code just above the // Resize Image line work to keep the transparency of the png images?

    imagealphablending($tmp, false );
    imagesavealpha($tmp, true );

    so would look like the following code?

    imagealphablending($tmp, false );
    imagesavealpha($tmp, true );

    // Resize Image

    if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.