Compress jpeg on server with PHP

前端 未结 4 561
天涯浪人
天涯浪人 2020-12-06 17:45

I have a site with about 1500 JPEG images, and I want to compress them all. Going through the directories is not a problem, but I cannot seem to find a function that compres

相关标签:
4条回答
  • 2020-12-06 17:52

    You will need to use the php gd library for that... Most servers have it installed by default. There are a lot of examples out there if you search for 'resize image php gd'.

    For instance have a look at this page http://911-need-code-help.blogspot.nl/2008/10/resize-images-using-phpgd-library.html

    0 讨论(0)
  • 2020-12-06 18:02

    The solution provided by vlzvl works well. However, using this solution, you can also overwrite an image by changing the order of the code.

        $image = imagecreatefromjpeg("image.jpg");  
        unlink("image.jpg");
       imagejpeg($image,"image.jpg",50);
    

    This allows you to compress a pre-existing image and store it in the same location with the same filename.

    0 讨论(0)
  • 2020-12-06 18:05

    you're not telling if you're using GD, so i assume this.

    $img = imagecreatefromjpeg("myimage.jpg");   // load the image-to-be-saved
    
    // 50 is quality; change from 0 (worst quality,smaller file) - 100 (best quality)
    imagejpeg($img,"myimage_new.jpg",50);
    
    unlink("myimage.jpg");   // remove the old image
    
    0 讨论(0)
  • 2020-12-06 18:07

    I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:

    $img = new Imagick();
    $img->readImage($src);
    $img->setImageCompression(Imagick::COMPRESSION_JPEG);
    $img->setImageCompressionQuality(90);
    $img->stripImage();
    $img->writeImage($dest); 
    $img->clean();
    
    0 讨论(0)
提交回复
热议问题