Home php教程 php手册 无损压缩图片心得(二)

无损压缩图片心得(二)

Jun 13, 2016 am 11:31 AM
upload two compression Participation picture exist deal with Experience Activity user of net

用户之前在发烧网参加的上传图片活动都没有经过无损压缩处理.想用脚本对一月内传上去的图片进行处理,但Amazon_S3服务集群上只能使用他们提供的一些简单的API.所以只能先down 下来,压缩处理后,再传上去覆盖原来的图片.

经过多次调试,最终写了个php的脚本对之进行处理:详见这里.

代码如下:

<span class="lnum">   1:  </span><?php
Copy after login
<span class="lnum">   2:  </span><span class="kwrd">function</span> compress_img ($source) {
Copy after login
<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>);
Copy after login
<span class="lnum">   4:  </span>    $start_time = strtotime(<span class="str">"-30 day"</span>);
Copy after login
<span class="lnum">   5:  </span>    exec(<span class="str">"s3cmd ls s3://fever38-us-static/hotdeals/{$source}/ > ./tmp.txt"</span>);
Copy after login
<span class="lnum">   6:  </span>    $rs = file(<span class="str">'./tmp.txt'</span>);
Copy after login
<span class="lnum">   7:  </span>&#160;
Copy after login
<span class="lnum">   8:  </span>    <span class="kwrd">foreach</span>($rs <span class="kwrd">as</span> $line) {
Copy after login
<span class="lnum">   9:  </span>        $r =  array_filter(explode(<span class="str">' '</span>, $line));
Copy after login
<span class="lnum">  10:  </span>        <span class="kwrd">if</span>(!empty($r[0])){
Copy after login
<span class="lnum">  11:  </span>            $r[0] = trim($r[0]);
Copy after login
<span class="lnum">  12:  </span>            $time = strtotime($r[0]);
Copy after login
<span class="lnum">  13:  </span>        }
Copy after login
<span class="lnum">  14:  </span>        <span class="kwrd">if</span>(!empty($time) && $time >= $start_time){
Copy after login
<span class="lnum">  15:  </span>            <span class="kwrd">if</span>(!empty($r[10])){
Copy after login
<span class="lnum">  16:  </span>                $img = trim($r[10]);
Copy after login
<span class="lnum">  17:  </span>                $path_info = pathinfo($r[10]);
Copy after login
<span class="lnum">  18:  </span>                $ext = trim($path_info[<span class="str">"extension"</span>]);
Copy after login
<span class="lnum">  19:  </span>                $file_name = strtolower(trim($path_info[<span class="str">"basename"</span>]));
Copy after login
<span class="lnum">  20:  </span>&#160;
Copy after login
<span class="lnum">  21:  </span>                exec(<span class="str">"s3cmd get "</span>.$img);
Copy after login
<span class="lnum">  22:  </span>                exec(<span class="str">"cp {$file_name} /mnt/heisoo/s3/{$source}/"</span>);
Copy after login
<span class="lnum">  23:  </span>&#160;
Copy after login
<span class="lnum">  24:  </span>                <span class="kwrd">if</span> (in_array($ext,$exts)) {
Copy after login
<span class="lnum">  25:  </span>                    system(<span class="str">"/usr/bin/optipng -o5 "</span>.$file_name);
Copy after login
<span class="lnum">  26:  </span>                }
Copy after login
<span class="lnum">  27:  </span>                <span class="kwrd">if</span> ($ext == <span class="str">"jpg"</span> || $ext == <span class="str">"jpeg"</span>) {
Copy after login
<span class="lnum">  28:  </span>                    system(<span class="str">"/usr/bin/jpegoptim -o --strip-all "</span>.$file_name);
Copy after login
<span class="lnum">  29:  </span>                }
Copy after login
<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>);
Copy after login
<span class="lnum">  31:  </span>                unlink($file_name);
Copy after login
<span class="lnum">  32:  </span>            }
Copy after login
<span class="lnum">  33:  </span>        }
Copy after login
<span class="lnum">  34:  </span>    }
Copy after login
<span class="lnum">  35:  </span>&#160;
Copy after login
<span class="lnum">  36:  </span>    unlink(<span class="str">'./tmp.txt'</span>);
Copy after login
<span class="lnum">  37:  </span>}
Copy after login
<span class="lnum">  38:  </span>&#160;
Copy after login
<span class="lnum">  39:  </span>compress_img(<span class="str">"promotion_main_pic"</span>);
Copy after login
<span class="lnum">  40:  </span>compress_img(<span class="str">"src_thumb"</span>);
Copy after login
<span class="lnum">  41:  </span>compress_img(<span class="str">"uploadImage"</span>);
Copy after login
<span class="lnum">  42:  </span>compress_img(<span class="str">"dialog_image"</span>);
Copy after login
<span class="lnum">  43:  </span>compress_img(<span class="str">"joinPicture"</span>);
Copy after login
<span class="lnum">  44:  </span>?>
Copy after login
Statement of this Website
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the &quot;Discover&quot; button in the lower right corner, and then select the &quot;Notes&quot; option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the &quot;Follow&quot; button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select &quot;Personal Information&quot;

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the &quot;My&quot; button in the lower right corner; (2) On the personal center page, find &quot;Settings&quot; and click it; (3) Scroll down and find the &quot;Clear Cache&quot; option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the &quot;Comment&quot; button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a &quot;picture&quot; button or a &quot;+&quot; button, click

Log in to Ubuntu as superuser Log in to Ubuntu as superuser Mar 20, 2024 am 10:55 AM

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

A complete guide to the activities of "Glory of Kings" A complete guide to the activities of "Glory of Kings" Mar 24, 2024 pm 12:36 PM

King of Glory has launched the Let’s Go Together to Flower Season event. Players who participate in the event can receive free avatar frames and many gifts. The event has a time limit and provides players with a total of four levels. Today, the editor has brought you a guide to the Let’s Go to Flower Season event. Encyclopedia, I hope it can help everyone complete the level challenge. A guide to the King of Glory's &quot;Going to the Flowering Season&quot; event. King of Glory, &quot;Going to the Flowering Season&quot; activity introduction. How to play: 1. &quot;Going to the Flowering Season&quot; is a card-turning activity, and players need to turn over the cards to pass the level. 2. Players can turn over cards by completing tasks and obtaining flower dew during the event. 3. Every four clearance cards in the activity panel are connected into a line (including horizontal lines, vertical lines and diagonal lines) to pass a small level. 4. Every time you clear a level, you can get corresponding rewards, and you can also get additional rewards by helping your friends turn over cards. live

7-zip maximum compression rate setting, how to compress 7zip to the minimum 7-zip maximum compression rate setting, how to compress 7zip to the minimum Jun 18, 2024 pm 06:12 PM

I found that the compressed package downloaded from a download website will be larger than the original compressed package after decompression. The difference is tens of Kb for a small one and several dozen Mb for a large one. If it is uploaded to a cloud disk or paid space, it does not matter if the file is small. , if there are many files, the storage cost will be greatly increased. I studied it specifically and can learn from it if necessary. Compression level: 9-Extreme compression Dictionary size: 256 or 384, the more compressed the dictionary, the slower it is. The compression rate difference is larger before 256MB, and there is no difference in compression rate after 384MB. Word size: maximum 273 Parameters: f=BCJ2, test and add parameter compression rate will be higher

See all articles