php替换图片alt与title标签

 更新时间:2016年11月25日 16:57  点击:2192

要做到替换所有图片或者是连接的alt title我们必须用到正则函数,下面看一个替换图片alt实例

*/

preg_replace('#alt="[^"]*"#i','alt="".$title.""',$str);

/*
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])


在 subject 中搜索 pattern 模式的匹配项并替换为 replacement。如果指定了 limit,则仅替换 limit 个匹配,如果省略 limit 或者其值为 -1,则所有的匹配项都会被替换。

 


过滤所有html 标签
*/

$html_body ="<br />fdafdsa <div>内容</div>";
preg_replace("/(</?)(w+)([^>]*>)/e",
             "'1'.strtoupper('2').'3'",
             $html_body);


   
//更多详细内容请查看:php教程er/php-function/33530.htm">http://www.111cn.net/phper/php-function/33530.htm

gd库是php教程中一个处理图像的专用库,他可以方便快捷的处理大多数据处理,它提供了大量的图片处理函数,下面我们就利用gd库的函数来生成缩略图。
测试代码

 

<?php
include('resizeimage.php');
if(!empty($_post)){
echo($filename.".jpg?cache=".rand(0,999999));
}
?>
<form name="test" action="?submit=true" enctype="multipart/form-data" method="post" >
<input type="file" name="image" size="50" value="浏览"><p>
<input type="submit" value="上传图片">
</form>

resizeimage.php 文件

<?php
$filename="image.thumb";
// 生成图片的宽度
$resizewidth=400;
// 生成图片的高度
$resizeheight=400;

function resizeimage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$resizewidth=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$resizeheight=true;
}
if($resizewidth && $resizeheight){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($resizewidth){
$ratio = $widthratio;
}elseif($resizeheight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg ($newim,$name . ".jpg");
imagedestroy ($newim);
}else{
imagejpeg ($im,$name . ".jpg");
}
}

if($_files['image']['size']){
if($_files['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_files['image']['tmp_name']);
}
if($im){
if(file_exists("$filename.jpg")){
unlink("$filename.jpg");
}
resizeimage($im,$resizewidth,$resizeheight,$filename);
imagedestroy ($im);
}
}
?>

<?php教程
$oimg = "test.jpg";//original image name
$classes = array('translation','autoheight','autowidth','stretch');//give classes for the new creating images' size which are defined in the specified ini file
$suffix = 'jpg';//the new image's suffix
$inifile = 'image.ini.php';

$size = getimagesize($oimg);
$x = $size[0]/$size[1];
$name = explode('.',$oimg);

if(!file_exists($inifile)) die('ini file does not exist!');
$cn = parse_ini_file($inifile,true);//parse the class style image size from ini file
foreach($classes as $class){
    foreach($cn as $k=>$v){
        if($k==$class){
            if($v['width'] && $v['height']){
                $thumbwidth = $v['width'];
                $thumbheight = $v['height'];
            }elseif($v['width']){
                $thumbwidth = $v['width'];
                $thumbheight = round($thumbwidth/$x);
            }elseif($v['height']){
                $thumbheight = $v['height'];
                $thumbwidth = round($thumbheight*$x);
            }else{
                $thumbwidth = $size[0];
                $thumbheight = $size[1];
            }
            break;
        }
    }
    if(!isset($thumbheight) && !isset($thumbwidth)) die('ini file settings error!');

    $nimg = $name[0].'_'.$class.'.'.$suffix;//new image file name
    $source = imagecreatefromjpeg($oimg);
    $thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
    imagecopyresampled($thumb,$source,0,0,0,0,$thumbwidth,$thumbheight,$size[0],$size[1]);

    if($suffix=='jpg') $method = 'imagejpeg';
    else $method='image'.$suffix;
    $method($thumb, $nimg);
    imagedestroy($thumb);//release the image source
    imagedestroy($source);
}
?>

缩放图片的php代码,其中变量classes是一个数组,可以选择任意多个ini文件中指定的设置:

这是一个ini配置文件,上面的代码就要读取这个文件中的图片位置,可以多个。

<?php /*
;translate the image format using the original image size
[translation]
width=0
height=0

;stretch the image to the specified size
[stretch]
width=800
height=600

;zoom the image to the specified width with height auto size
[autoheight]
width=740
height=0

;zoom the image to the specified height with width auto size
[autowidth]
width=0
height=380
*/ ?>

注意:ini文件使用php解释时为注释文件,什么也没有输出,这是为了安全起见而故意为之。而;则是ini文件的注释。

先了解files函数

$_files数组内容如下:

$_files['myfile']['name']   客户端文件的原名称。
$_files['myfile']['type']   文件的 mime 类型,需要浏览器提供该信息的支持,例如"image/gif"。
$_files['myfile']['size']   已上传文件的大小,单位为字节。
$_files['myfile']['tmp_name']   文件被上传后在服务端储存的临时文件名,一般是系统默认。可以在php教程.ini的upload_tmp_dir 指定,但 用 putenv() 函数设置是不起作用的。
$_files['myfile']['error']   和该文件上传相关的错误代码。['error'] 是在 php 4.2.0 版本中增加的。下面是它的说明:(它们在php3.0以后成了常量)
  upload_err_ok
    值:0; 没有错误发生,文件上传成功。
  upload_err_ini_size
    值:1; 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
  upload_err_form_size
    值:2; 上传文件的大小超过了 html 表单中 max_file_size 选项指定的值。
  upload_err_partial
    值:3; 文件只有部分被上传。
  upload_err_no_file
    值:4; 没有文件被上传。
    值:5; 上传文件大小为0.

 

php代码

 

<?php
     $mkdir_file_dir    = mkdir('./img/'.$_post['title'],0777);           //上传文件的时候就开始创建一个图片相关的目录
     $tmp_file_name     = $_files['file']['tmp_name'];                        //上传成功之后取的临时文件名
     $file_name             = $_files['file']['name'];                        //原始的文件名
   
     $file_dir     = './img/'.$_post['title'].'/';                            //把创建的一个目录赋值给你一个变量作为最终的保存目录
    
     if(is_dir($file_dir))
     {
         move_uploaded_file($tmp_file_name,$file_dir.$file_name);        //开始移动文件
     }
 ?>

html代码

<html>
    <head>
        <title>
            my is upfile app!!
        </title>
        <meta    http-equiv="content-type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <form enctype="multipart/form-data" method="post" action="upfile_add.php">
                标题:     <input type="text" name="title" />
                上传文件: <input type="file" name="file" />
                <input type="submit" vlaue="提交" />
        </form>
    </body>
</html>

非常重要的一点是:文件通过post提交后是保存在c:windowstemp临时文件夹中通过$_files["photo"]["tmp_name"],我们可以轻松获得上传的临时文件,将它保存到我们指定的路径中 下面是解决方案move_uploaded_file($_files["photo"]["tmp_name"],$path)

图片按在比较进行放大缩小,这得利用php教程 gd库的函数现实现,我们会利用到imagecreatetruecolor(),imagecopyresampled()来操作

function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
    if($dst_width <1 || $dst_height <1) {
        echo "params width or height error !";
        exit();
    }
    if(!file_exists($src_file)) {
        echo $src_file . " is not exists !";
        exit();
    }

    $type=exif_imagetype($src_file);
    $support_type=array(imagetype_jpeg , imagetype_png , imagetype_gif);

    if(!in_array($type, $support_type,true)) {
        echo "this type of image does not support! only support jpg , gif or png";
        exit();
    }

    switch($type) {
        case imagetype_jpeg :
            $src_img=imagecreatefromjpeg($src_file);
            break;
        case imagetype_png :
            $src_img=imagecreatefrompng($src_file);        
            break;
        case imagetype_gif :
            $src_img=imagecreatefromgif($src_file);
            break;
        default:
            echo "load image error!";
            exit();
    }
    $src_w=imagesx($src_img);
    $src_h=imagesy($src_img);
    $ratio_w=1.0 * $dst_width/$src_w;
    $ratio_h=1.0 * $dst_height/$src_h;
    if ($src_w<=$dst_width && $src_h<=$dst_height) {
        $x = ($dst_width-$src_w)/2;
        $y = ($dst_height-$src_h)/2;
        $new_img=imagecreatetruecolor($dst_width,$dst_height);
        imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
        switch($type) {
            case imagetype_jpeg :
                imagejpeg($new_img,$dst_file,100);
                break;
            case imagetype_png :
                imagepng($new_img,$dst_file);
                break;
            case imagetype_gif :
                imagegif($new_img,$dst_file);
                break;
            default:
                break;
        }
    } else {
        $dstwh = $dst_width/$dst_height;
        $srcwh = $src_w/$src_h;
        if ($ratio_w <= $ratio_h) {
            $zoom_w = $dst_width;
            $zoom_h = $zoom_w*($src_h/$src_w);
        } else {
            $zoom_h = $dst_height;
            $zoom_w = $zoom_h*($src_w/$src_h);
        }

        $zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
        imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
        $new_img=imagecreatetruecolor($dst_width,$dst_height);
        $x = ($dst_width-$zoom_w)/2;
        $y = ($dst_height-$zoom_h)/2+1;
        imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
        switch($type) {
            case imagetype_jpeg :
                imagejpeg($new_img,$dst_file,100);
                break;
            case imagetype_png :
                imagepng($new_img,$dst_file);
                break;
            case imagetype_gif :
                imagegif($new_img,$dst_file);
                break;
            default:
                break;
        }
    }
}


总结,我们要生成比例生成小图就利用$dstwh = $dst_width/$dst_height;
        $srcwh = $src_w/$src_h;进行判断,然后再生成。

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • Jquery 获取指定标签的对象及属性的设置与移除

    1、先讲讲JQuery的概念,JQuery首先是由一个 America 的叫什么 John Resig的人创建的,后来又很多的JS高手也加入了这个团队。其实 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用简单的一些代...2014-05-31
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • JS创建Tag标签的方法详解

    这篇文章主要介绍了JS创建Tag标签的方法,结合具体实例形式分析了javascript动态操作页面HTML元素实现tag标签功能的步骤与相关操作技巧,需要的朋友可以参考下...2017-06-15
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • C#将图片和字节流互相转换并显示到页面上

    本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25