微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

浅谈利用PHP生成ICO图标的方法

本篇文章给大家介绍一下使用PHP生成ico图标的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

ico图标在每个网站中都需要用到的,使用方法也是很简单的,基本上以下面的方式为主,但也有其他的方式。

<link rel=shortcut icon href=/favicon.ico />

一般将ico图标放置在网站的根目录下,使用时可以使用相对路径,也可以使用绝对路径来引入,这个没有明确的规定。

我们先来了解几个函数,在下面的代码中要用到的,主要是一下图像处理函数

strtolower、end、imagecreatefromjpeg、imagecreatefromgif、imagecreatefrompng、getimagesize、imagecreatetruecolor、imagecopyresampled、imagejpeg、imagedestroy

一、strtolower函数

strtolower函数是将所有字符串全部转换为小写形式,版本支持PHP4.0+,使用方式:

<?PHP
echo strtolower('This is strtolower function');
?>

二、end函数

end函数是将数组内部指针指向最后一个元素,并返回该元素的值,版本支持PHP4.0+,使用方式:

<?PHP
$info = array('red','yellow','blue','white');
echo end($info);
?>

三、imagecreatefromjpeg函数

imagecreatefromjpeg函数是载入jpg 或 jpeg格式,成功后返回图象资源,失败后返回 FALSE 。PHP.net上给出的说明为:由文件或 URL 创建一个新图象,版本支持4.3+,使用方式:

<?PHP
$imgPath = './demo.jpg';
$im = @imagecreatefromjpeg($imgPath);
?>

四、imagecreatefromgif函数

使用方式及方法类似于imagecreatefromjpeg()函数

五、imagecreatefrompng函数

使用方式及方法类似于imagecreatefromjpeg()函数

六、getimagesize函数

getimagesize函数,取得图像大小,支持 JPC,JP2,JPX,JB2,XBM ,WBMP ,SWC ,TIFF等格式,成功则图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 IMG 标记中的 height/width 文本字符串,失败将返回false及警告。个人建议PHP版本高于5.0,使用方式:

<?PHP
$imgPath = './demo.png';
$info = getimagesize($imgPath);
?>

七、imagecreatetruecolor函数

imagecreatetruecolor函数,新建一个彩色图像,成功后返回图象资源,失败后返回 FALSE 。使用方式:

<?PHP
$tmp = imagecreatetruecolor($width, $height);
?>

八、imagecopyresampled函数

imagecopyresampled函数,重采样拷贝部分图像并调整大小,将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。

<?PHP
// 参数注释:目标图象连接资源,源图象连接资源,目标 X 坐标点,目标 Y 坐标点,源的 X 坐标点,源的 Y 坐标点,目标宽度,目标高度,源图象的宽度,源图象的高度
imagecopyresampled($tmp, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
?>

九、imagejpeg函数

imagejpeg函数,将图像输出到浏览器或文件PHP版本支持4.0+,使用方式:

<?PHP
// 参数注释:图像资源,路径,质量(认值(-1)使用认的IJG质量值(约75))
imagejpeg($tmp, $directory . $filename, 100);
?>

十、imagedestroy函数

imagedestroy函数,销毁图像,释放关联内存。PHP版本支持4.0+,使用方式:

<?PHP
imagedestroy('./demo.png');
?>

上面一共是10个函数,对于一些基本的函数没有做介绍,下面是生成ico代码

/**
 * 创建ico图标
 * @return string
 *
 */
public function icon()
{
    if(request()->isPost()) {
        //获取图片信息
        $postvars = [image => trim($_FILES[image][name]), image_tmp => $_FILES[image][tmp_name], image_size => (int)$_FILES[image][size], image_dimensions => (int)$_POST[image_dimensions]];
        //设置图片格式
        $validExts = [jpg, jpeg, gif, png];
        //设置图片文件大小      175kb
        $max_file_size = 179200;
        //图片名称和类型
        $filenameParts = explode(., $postvars[image]);
        //获取图片格式
        $ext = strtolower(end($filenameParts));
        //设置图片存储路径
        $directory = ./favicon/;
        //设置命名格式
        $rand     = time();
        $filename = $rand . $postvars[image_size];
        //判断图片大小
        if($postvars[image_size] <= $max_file_size) {
            //判断图片格式
            if(in_array($ext, $validExts)) {
                //依据格式使用不同函数
                if($ext == jpg || $ext == jpeg) {
                    $image = imagecreatefromjpeg($postvars[image_tmp]);
                }
                else if($ext == gif) {
                    $image = imagecreatefromgif($postvars[image_tmp]);
                }
                else if($ext == png) {
                    $image = imagecreatefrompng($postvars[image_tmp]);
                }
                if($image) {
                    list($width, $height) = getimagesize($postvars[image_tmp]);
                    $newWidth  = $postvars[image_dimensions];
                    $newHeight = $postvars[image_dimensions];
                    $tmp       = imagecreatetruecolor($newWidth, $newHeight);
                    //将图像复制到具有新宽度和高度的图像
                    imagecopyresampled($tmp, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
                    //创建100%质量的图像文件
                    if(is_dir($directory)) {
                        if(is_writable($directory)) {
                            imagejpeg($tmp, $directory . $filename, 100) or die('没有创建文件的权限');
                            if(file_exists($directory . $filename)) {
                                //重命名
                                $newFilename = md5(time());
                                rename($directory . $filename, $directory . $newFilename . .ico);
                                return 'http://'.$_SERVER['SERVER_NAME'] . substr($directory, 1).$newFilename..ico;
                            }
                            else {
                                echo 指定的文件不可写;
                            }
                        }
                        else {
                            return '目录:' . $directory . '无写入权限';
                        }
                    }
                    else {
                        return '目录: ' . $directory . '不存在';
                    }
                    imagedestroy($image);
                    imagedestroy($tmp);
                }
                else {
                    return 无法创建图像文件;
                }
            }
            else {
                return 图标过大,不能超过175KB;
            }
        }
        else {
            return 图片格式只能是后面几种 (jpg, jpeg, gif, png).;
        }
    }
    else {
        return view('Index/icon');
    }
}

推荐学习:《PHP教程

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐