ICO图标在线生成,php生成ICO图标在线制作源码

我们做web系统的时候,每个浏览器的tab这里都会有一个图标,这个图标叫favicon图标,favicon.ico文件放在系统的根目录

如果程序员没有ICO制作工具,那么要如何生成图标呢?可以用程序来实现生成啊!也可以把图标裁剪好,选择在线生成,在线生成测试地址:www.vsoyo.com/ico

下面和大家一起探讨下,php生成ICO图标在线制作源码,很简单的原理,请看如下代码

下面是ICO图标在线生成的主要代码

<?php
if(!function_exists("generate_favicon")){
function generate_favicon(){
    // Create favicon.
    $postvars = array(
        "image"             => trim($_FILES["image"]["name"]),
        "image_tmp"            => $_FILES["image"]["tmp_name"],
        "image_size"        => (int)$_FILES["image"]["size"],
        "image_dimensions"    => (int)$_POST["image_dimensions"]);

    // Provide valid extensions and max file size
    $valid_exts = array("jpg","jpeg","gif","png");
    $max_file_size = 179200; // 175kb

    $filenameParts = explode(".",$postvars["image"]);
    $ext = strtolower(end($filenameParts));
    $directory = "./favicon/"; // Directory to save favicons. Include trailing slash.
    $rand = rand(1000,9999);
    $filename = $rand.$postvars["image"];

    // Check not larger than max size.
    if($postvars["image_size"] <= $max_file_size){
        // Check is valid extension.
        if(in_array($ext,$valid_exts)){
            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);
                    
                // Copy the image to one with the new width and height.
                imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
            
                // Create image file with 100% quality.
                if(is_dir($directory)){
                    if(is_writable($directory)){
                        imagejpeg($tmp,$directory.$filename,100) or die('Could not make image file');
                        if(file_exists($directory.$filename)){    
                            // Image created, now rename it to its
                            $ext_pos = strpos($rand.$postvars["image"],"." . $ext);
                            $strip_ext = substr($rand.$postvars["image"],0,$ext_pos);
                            // Rename image to .ico file
                            rename($directory.$filename,$directory.$strip_ext.".ico");
                            return '<strong>图标预览:</strong><br/>
                            <img src="'.$directory.$strip_ext.'.ico" border="0" title="Favicon 完毕预览" style="padding: 4px 0px 4px 0px;background-color:#e0e0e0" /><br/>
                            Favicon/ICO图标制作完毕: <a href="'.$directory.$strip_ext.'.ico" target="_blank" name="右键另存为下载!">点击/右键另存下载,后重命名为"favicon.ico"</a>';
                        } else {
                            "File was not created.";
                        }
                    } else {
                        return 'The directory: "'.$directory.'" is not writable.';
                    }
                } else {
                    return 'The directory: "'.$directory.'" is not valid.';
                }
            
                imagedestroy($image);
                imagedestroy($tmp);
            } else {
                return "Could not create image file.";
            }
        } else {
            return "图标过大,不能超过175KB";    
        }
    } else {
        return "图片格式只能是后面几种 (jpg, jpeg, gif, png).";    
    }
}
}

?>

需要进一步技术交流的朋友 ,请加我微信吧:15889726201

转载于:https://www.cnblogs.com/keleyu/p/9447835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值