search
HomeBackend DevelopmentPHP TutorialExperience on Lossless Compression of Images (2)_PHP Tutorial
Experience on Lossless Compression of Images (2)_PHP TutorialJul 20, 2016 am 11:15 AM
uploadtwocompressionParticipationpictureexistdeal withExperienceActivityuserofnet

None of the image upload activities that the user participated in before on Fever.com have been processed by lossless compression. I want to use a script to process the images uploaded within one month, but the Amazon_S3 service cluster can only use some simple ones they provide. API. So we can only download it first, compress it, and then upload it to overwrite the original image.

After many debuggings, I finally wrote a php script to process it: see here for details.

The code is as follows:

<span class="lnum">   1:  </span><?php
<span class="lnum">   2:  </span><span class="kwrd">function</span> compress_img ($source) {
<span class="lnum">   3:  </span>    $exts = array(<span class="str">"png"</span>,<span class="str">"bmp"</span>,<span class="str">"gif"</span>,<span class="str">"pnm"</span>,<span class="str">"tiff"</span>);
<span class="lnum">   4:  </span>    $start_time = strtotime(<span class="str">"-30 day"</span>);
<span class="lnum">   5:  </span>    exec(<span class="str">"s3cmd ls s3://fever38-us-static/hotdeals/{$source}/ > ./tmp.txt"</span>);
<span class="lnum">   6:  </span>    $rs = file(<span class="str">'./tmp.txt'</span>);
<span class="lnum">   7:  </span>&#160;
<span class="lnum">   8:  </span>    <span class="kwrd">foreach</span>($rs <span class="kwrd">as</span> $line) {
<span class="lnum">   9:  </span>        $r =  array_filter(explode(<span class="str">' '</span>, $line));
<span class="lnum">  10:  </span>        <span class="kwrd">if</span>(!empty($r[0])){
<span class="lnum">  11:  </span>            $r[0] = trim($r[0]);
<span class="lnum">  12:  </span>            $time = strtotime($r[0]);
<span class="lnum">  13:  </span>        }
<span class="lnum">  14:  </span>        <span class="kwrd">if</span>(!empty($time) && $time >= $start_time){
<span class="lnum">  15:  </span>            <span class="kwrd">if</span>(!empty($r[10])){
<span class="lnum">  16:  </span>                $img = trim($r[10]);
<span class="lnum">  17:  </span>                $path_info = pathinfo($r[10]);
<span class="lnum">  18:  </span>                $ext = trim($path_info[<span class="str">"extension"</span>]);
<span class="lnum">  19:  </span>                $file_name = strtolower(trim($path_info[<span class="str">"basename"</span>]));
<span class="lnum">  20:  </span>&#160;
<span class="lnum">  21:  </span>                exec(<span class="str">"s3cmd get "</span>.$img);
<span class="lnum">  22:  </span>                exec(<span class="str">"cp {$file_name} /mnt/heisoo/s3/{$source}/"</span>);
<span class="lnum">  23:  </span>&#160;
<span class="lnum">  24:  </span>                <span class="kwrd">if</span> (in_array($ext,$exts)) {
<span class="lnum">  25:  </span>                    system(<span class="str">"/usr/bin/optipng -o5 "</span>.$file_name);
<span class="lnum">  26:  </span>                }
<span class="lnum">  27:  </span>                <span class="kwrd">if</span> ($ext == <span class="str">"jpg"</span> || $ext == <span class="str">"jpeg"</span>) {
<span class="lnum">  28:  </span>                    system(<span class="str">"/usr/bin/jpegoptim -o --strip-all "</span>.$file_name);
<span class="lnum">  29:  </span>                }
<span class="lnum">  30:  </span>                system(<span class="str">"s3cmd put {$file_name} {$img} --guess-mime-type --add-header 'Cache-Control:max-age=31536000' --add-header 'Expires: Thu, 01 Dec 2014 16:00:00 GMT' --acl-public"</span>);
<span class="lnum">  31:  </span>                unlink($file_name);
<span class="lnum">  32:  </span>            }
<span class="lnum">  33:  </span>        }
<span class="lnum">  34:  </span>    }
<span class="lnum">  35:  </span>&#160;
<span class="lnum">  36:  </span>    unlink(<span class="str">'./tmp.txt'</span>);
<span class="lnum">  37:  </span>}
<span class="lnum">  38:  </span>&#160;
<span class="lnum">  39:  </span>compress_img(<span class="str">"promotion_main_pic"</span>);
<span class="lnum">  40:  </span>compress_img(<span class="str">"src_thumb"</span>);
<span class="lnum">  41:  </span>compress_img(<span class="str">"uploadImage"</span>);
<span class="lnum">  42:  </span>compress_img(<span class="str">"dialog_image"</span>);
<span class="lnum">  43:  </span>compress_img(<span class="str">"joinPicture"</span>);
<span class="lnum">  44:  </span>?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440176.htmlTechArticleNone of the image upload activities that users participated in before on Fever.com have been processed by lossless compression. I want to use a script to compress the images within one month. The uploaded images are processed, but only he can be used on the Amazon_S3 service cluster...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 Windows 11 上启用或禁用内存压缩功能如何在 Windows 11 上启用或禁用内存压缩功能Sep 19, 2023 pm 11:33 PM

使用Windows11上的内存压缩,即使RAM量有限,您的设备也会窒息运行。在本文中,我们将向您展示如何在Windows11上启用或禁用内存压缩。什么是内存压缩?内存压缩是一种在将数据写入RAM之前压缩数据的功能,从而在其上提供更多存储空间。当然,存储在物理内存中的更多数据转化为更快的系统运行和更好的整体性能。此功能在Windows11中默认启用,但如果它以某种方式未处于活动状态,您可以禁用或重新启用它。如何在Windows11中启用内存压缩?单击搜索栏,键入powershell,然后从结果中单

pr文件的压缩类型不受支持怎么办pr文件的压缩类型不受支持怎么办Mar 23, 2023 pm 03:12 PM

pr文件的压缩类型不受支持的原因及解决办法:1、精简版pr把许多视频编码器精简掉了,重新安装使用完整版Premiere;2、视频编码不规范导致的,可以通过格式工厂,将视频转换成WMV格式即可。

如何使用Nginx进行HTTP请求的压缩和解压缩如何使用Nginx进行HTTP请求的压缩和解压缩Aug 02, 2023 am 10:09 AM

如何使用Nginx进行HTTP请求的压缩和解压缩Nginx是一款高性能的Web服务器和反向代理服务器,其功能强大且灵活。在处理HTTP请求时,可以使用Nginx提供的gzip和gunzip模块对请求进行压缩和解压缩,以减小数据传输量,提高请求响应速度。本文将介绍如何使用Nginx进行HTTP请求的压缩和解压缩的具体步骤,并提供相应的代码示例。配置gzip模块

linux怎么显示压缩文件信息linux怎么显示压缩文件信息Feb 13, 2023 am 10:20 AM

显示方法:1、用Vim编辑器,语法“vim 压缩文件”;2、用“tar -tf 压缩文件”命令;3、用“rar v 压缩文件”命令;4、用“unrar l 压缩文件”命令;5、用“zip -sf 压缩文件”命令;6、用“unzip -l 压缩文件”命令;7、用“zipinfo 压缩文件”命令;8、用“zcat 压缩文件”命令;9、用“zless 压缩文件”;10、用less。

Golang实现图片质量压缩的方法Golang实现图片质量压缩的方法Aug 17, 2023 pm 10:28 PM

Golang实现图片质量压缩的方法随着互联网的迅猛发展,图片已经成为了人们网上交流和信息传递的重要媒介之一。然而,高分辨率的图片不仅会占用大量的存储空间,而且在网络传输时也会增加加载的时间,给用户体验带来一定的影响。因此,在实际应用中,对图片进行压缩是一项非常有意义的工作。本文将介绍如何使用Golang实现对图片质量进行压缩的方法。首先,我们需要导入Gola

Linux服务器日志过大,如何解决?Linux服务器日志过大,如何解决?Jun 29, 2023 pm 11:09 PM

Linux服务器上常见的日志文件太大问题及其解决方法随着互联网的快速发展和服务器的广泛应用,服务器日志文件越来越大成为一个常见的问题。大量的日志数据不仅占用磁盘空间,还可能影响服务器的性能和运行稳定性。本文将讨论Linux服务器上常见的日志文件太大问题,并提供一些解决方法。一、常见的日志文件在Linux服务器上,常见的日志文件包括系统日志、应用程序日志、We

如何使用Java中的Zip函数进行文件压缩如何使用Java中的Zip函数进行文件压缩Jun 26, 2023 pm 02:10 PM

压缩文件是一种常见的操作,能够节省磁盘的空间以及网络传输的时间,而Java中提供了Zip函数用于实现文件的压缩。本文将通过详细的介绍和实例演示来展示如何使用Java中的Zip函数进行文件压缩。一、Zip函数介绍Zip函数是Java中提供的压缩和打包工具类库,使用该函数可以将文件或文件夹压缩成一个Zip格式的文件。Zip函数中主要使用了ZipOutputStr

MySQL中如何实现数据的压缩和解压缩?MySQL中如何实现数据的压缩和解压缩?Jul 30, 2023 pm 01:43 PM

MySQL是一种被广泛使用的关系型数据库管理系统,支持数据的压缩和解压缩功能。在大规模数据存储和处理中,数据的压缩可以显著减少存储空间占用以及提高数据的传输效率。本文将介绍MySQL中如何实现数据的压缩和解压缩,并给出相应的代码示例。一、压缩数据MySQL提供了多种压缩算法,例如LZ4、Zlib、Snappy等。在MySQL5.7.17及以后的版本中,In

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.