分享一段程序代码:用PHP做图片锐化程序[绝对好用]

2024-02-08 06:18

本文主要是介绍分享一段程序代码:用PHP做图片锐化程序[绝对好用],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

做相册缩略图程序,上传后图片要自动生成缩略图,并且要锐化一下。
上网搜索了一下,结果效果一点也不好,找来找去,天下怎么都是一样的代码?
没办法,只好去老外那里淘一淘了,结果发现这个非常不错,现在分享给大家了,呵呵。

<?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'); 

?> 

这篇关于分享一段程序代码:用PHP做图片锐化程序[绝对好用]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/690133

相关文章

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

MySQL8.2.0安装教程分享

《MySQL8.2.0安装教程分享》这篇文章详细介绍了如何在Windows系统上安装MySQL数据库软件,包括下载、安装、配置和设置环境变量的步骤... 目录mysql的安装图文1.python访问网址2javascript.点击3.进入Downloads向下滑动4.选择Community Server5.

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

CentOS系统Maven安装教程分享

《CentOS系统Maven安装教程分享》本文介绍了如何在CentOS系统中安装Maven,并提供了一个简单的实际应用案例,安装Maven需要先安装Java和设置环境变量,Maven可以自动管理项目的... 目录准备工作下载并安装Maven常见问题及解决方法实际应用案例总结Maven是一个流行的项目管理工具

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

10个Python自动化办公的脚本分享

《10个Python自动化办公的脚本分享》在日常办公中,我们常常会被繁琐、重复的任务占据大量时间,本文为大家分享了10个实用的Python自动化办公案例及源码,希望对大家有所帮助... 目录1. 批量处理 Excel 文件2. 自动发送邮件3. 批量重命名文件4. 数据清洗5. 生成 PPT6. 自动化测试

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo