分享一段程式碼:用PHP做圖片銳利化程式[絕對好用]

來源:互聯網
上載者:User

做相簿縮圖程式,上傳後圖片要自動產生縮圖,並且要銳利化一下。
上網搜尋了一下,結果效果一點也不好,找來找去,天下怎麼都是一樣的代碼?
沒辦法,只好去老外那裡淘一淘了,結果發現這個非常不錯,現在分享給大家了,呵呵。

<?php  

    function GDThrowError($message) 
    { 
        // don't throw plain text errors in a function that's supposed to return an image 
        // as per "the principle of least astonishment": the user is expecting 
        // a jpeg, and they'll be less astonished to get a JPEG error message 
        // than they would be if they got plain text.  Imagine this was called 
        // from an image tag, which makes sense, that's how you use images: 
        // plain text errors won't even reach most users, they'd have to copy 
        // the img src into the address bar.  It probably wont' even occur to 
        // them, as it's not typically helpful to view the source of an image 
        // resource. 
        $font = 2; 
        // create a canvas with a bit of padding 
        $errimg = imagecreate((imagefontwidth($font) * strlen($message)) + 20, imagefontheight($font) + 10); 
        $bg = imagecolorallocate($errimg, 255, 255, 255); 
        $textcol = imagecolorallocate($errimg, 0, 0, 0); 
        imagestring($errimg, 2, 10, 5, $message, $textcol); 
        header('Content-type: image/jpeg');  
        imagejpeg($errimg); 
        imagedestroy($errimg); 
    } 

    function GDMakeJpegLookLikeCrap($target) 
    { 
        // image dimensions are no longer needed (see below), but getimagesize can do some simple validation 
        if (($dims = @getimagesize($target)) === false || $dims['mime'] != 'image/jpeg') 
        { 
            GDThrowError('The file you specified couldn\'t be found or is not a valid jpeg image.  Make sure you spelled it correctly and provided the correct path.'); 
           return(false); 
        } 
        // the original function creates a new image and resamples the source to it using the same height and width here. 
        // I imagine this was to add future resizing functionality but as it is it does nothing but waste resources 
        $image = imagecreatefromjpeg($target); 
        // don't really know/care what this is.  If you're interested see http://us2.php.net/imageconvolution         
        // try tweaking these three vars for different effects, but there is a sharpening function in the php docs (above links) and it's not a trivial operation 
        $spnMatrix = array( array(-1,-1,-1,), 
                            array(-1,16,-1,), 
                            array(-1,-1,-1));  
        $divisor = 8;  
        $offset = 0;  
        imageconvolution($image, $spnMatrix, $divisor, $offset);  
        // I like to send headers as late as possible to avoid already sent errors and duplicate header content 
        header('Content-type: image/jpeg');  
        imagejpeg($image, null, 100);  
        imagedestroy($image);   



// example call 
$s_image = (isset($_GET['image'])) ? $_GET['image'] : null; 

if (preg_match('/\.(jpg|jpeg)$/i', $s_image))   

    GDMakeJpegLookLikeCrap($s_image);   

else 

    GDThrowError('Please specify a jpeg file to sharpen in the form: ' . $_SERVER['PHP_SELF'] . '?image=filename.jpg'); 

?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.