首页 > 编程知识 正文

php图片最宽600像素(6000像素图片)

时间:2023-12-02 21:07:33 阅读:311572 作者:KBVE

本文目录一览:

  • 1、用php正则表达式将css文件里的宽度小于600px的像素值增加1.5倍
  • 2、请高手帮帮忙! 怎样在php中调整图片显示的大小?
  • 3、如何用PHP生成小图
  • 4、php 中 图片如何输出多尺寸
  • 5、thinkphp 如何生产指定图片尺寸大小的?
  • 6、PHP图片生成

用php正则表达式将css文件里的宽度小于600px的像素值增加1.5倍

css文件(style.css)

.div1 {

    width: 800px;

    height: 322px;

}

.div2 {

    width: 30px;

    height: 14px;

}

php文件:

$css_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'style.css';

if (file_exists($css_file)){

$css_content = file_get_contents($css_file);

$css_content = preg_replace_callback('/width:s*(d+)px/im', 'callback_replace_width', $css_content);

file_put_contents($css_file, $css_content);

}

function callback_replace_width( $matches ){

if($matches[1]600){

$matches[0] = 'width: ' . ($matches[1] * 1.5) . 'px';

};

return $matches[0];

}

输出结果:

.div1 {

    width:800px;

    height: 322px;

}

.div2 {

    width: 45px;

    height: 14px;

}

请高手帮帮忙! 怎样在php中调整图片显示的大小?

你在显示的时候,直接指定图片显示的大小是不会改变你的源文件大小的。

如:

img src='/upload/1.jpg' width='100' / 那么显示的时候,就是宽度100,高度会根据你的图片的尺寸缩放。

如何用PHP生成小图

上传文件支持辨别大写+小写,不再是只认小写不认大写。

支持中文文件名,上传后写入不再为乱码。

网页为UTF-8

运行请打开php.ini里的GD库和MB库,第一个处理图像用,第二个处理内码用。

主目录下有

ratioimg.html

ratioimg.php

image文件夹,运行请新建此文件夹

ratioimg.html

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

title上传图片/title

/head

body

form id="ratioimg" name="ratioimg" enctype="multipart/form-data" method="post" action="ratioimg.php"

label

input type="file" name="file" /

/label

p

label

input type="submit" name="Submit" value="提交" /

/label

/p

/form

/body

/html

ratioimg.php

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

title缩略图生成/title

/head

?

/***************************************/

/*功 能:利用PHP的GD库生成高质量的缩略图*/

/*运行环境:PHP5.01/GD2*/

/*类说明:可以选择是/否裁图。

如果裁图则生成的图的尺寸与您输入的一样。

原则:尽可能多保持原图完整

如果不裁图,则按照原图比例生成新图

原则:根据比例以输入的长或者宽为基准*/

/*参 数:$img:源图片地址

$wid:新图的宽度

$hei:新图的高度

$c:是否裁图,1为是,0为否*/

/* Author: antplus */

/* version: 1.1 */

/* QQ: 38188141 */

/* MSN: [email]antplus@163.net[/email] */

/***************************************/

class resizeimage

{

//图片类型

var $type;

//实际宽度

var $width;

//实际高度

var $height;

//改变后的宽度

var $resize_width;

//改变后的高度

var $resize_height;

//是否裁图

var $cut;

//源图象

var $srcimg;

//目标图象地址

var $dstimg;

//临时创建的图象

var $im;

function resizeimage($img, $wid, $hei,$c)

{

//echo $img.$wid.$hei.$c;

$this-srcimg = $img;

$this-resize_width = $wid;

$this-resize_height = $hei;

$this-cut = $c;

//图片的类型

$this-type = substr(strrchr($this-srcimg,"."),1);

//初始化图象

$this-initi_img();

//目标图象地址

$this - dst_img();

//imagesx imagesy 取得图像 宽、高

$this-width = imagesx($this-im);

$this-height = imagesy($this-im);

//生成图象

$this-newimg();

ImageDestroy ($this-im);

}

function newimg()

{

// +----------------------------------------------------+

// | 增加LOGO到缩略图中 Modify By Ice

// +----------------------------------------------------+

//Add Logo

//$logoImage = ImageCreateFromJPEG('t_a.jpg');

//ImageAlphaBlending($this-im, true);

//$logoW = ImageSX($logoImage);

//$logoH = ImageSY($logoImage);

// +----------------------------------------------------+

//改变后的图象的比例

$resize_ratio = ($this-resize_width)/($this-resize_height);

//实际图象的比例

$ratio = ($this-width)/($this-height);

if(($this-cut)=="1")

//裁图

{

if($ratio=$resize_ratio)

//高度优先

{

//imagecreatetruecolor — 新建一个真彩色图像

$newimg = imagecreatetruecolor($this-resize_width,$this-resize_height);

//imagecopyresampled — 重采样拷贝部分图像并调整大小

imagecopyresampled($newimg, $this-im, 0, 0, 0, 0, $this-resize_width,$this-resize_height, (($this-height)*$resize_ratio), $this-height);

// +----------------------------------------------------+

// | 增加LOGO到缩略图中 Modify By Ice

// +----------------------------------------------------+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +----------------------------------------------------+

//imagejpeg — 以 JPEG 格式将图像输出到浏览器或文件

ImageJpeg ($newimg,$this-dstimg);

echo "缩略图生成成功!";

}

if($ratio$resize_ratio)

//宽度优先

{

$newimg = imagecreatetruecolor($this-resize_width,$this-resize_height);

imagecopyresampled($newimg, $this-im, 0, 0, 0, 0, $this-resize_width, $this-resize_height, $this-width, (($this-width)/$resize_ratio));

// +----------------------------------------------------+

// | 增加LOGO到缩略图中 Modify By Ice

// +----------------------------------------------------+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +----------------------------------------------------+

ImageJpeg ($newimg,$this-dstimg);

echo "缩略图生成成功!";

}

}

else

//不裁图

{

if($ratio=$resize_ratio)

{

$newimg = imagecreatetruecolor($this-resize_width,($this-resize_width)/$ratio);

imagecopyresampled($newimg, $this-im, 0, 0, 0, 0, $this-resize_width, ($this-resize_width)/$ratio, $this-width, $this-height);

// +----------------------------------------------------+

// | 增加LOGO到缩略图中 Modify By Ice

// +----------------------------------------------------+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +----------------------------------------------------+

ImageJpeg ($newimg,$this-dstimg);

echo "缩略图生成成功!";

}

if($ratio$resize_ratio)

{

$newimg = imagecreatetruecolor(($this-resize_height)*$ratio,$this-resize_height);

imagecopyresampled($newimg, $this-im, 0, 0, 0, 0, ($this-resize_height)*$ratio, $this-resize_height, $this-width, $this-height);

// +----------------------------------------------------+

// | 增加LOGO到缩略图中 Modify By Ice

// +----------------------------------------------------+

//ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

// +----------------------------------------------------+

ImageJpeg ($newimg,$this-dstimg);

echo "缩略图生成成功!";

}

}

// +----------------------------------------------------+

// | 释放资源 Modify By Ice

// +----------------------------------------------------+

//ImageDestroy($logoImage);

// +----------------------------------------------------+

}

//初始化图象

function initi_img()

{

if($this-type=="jpg")

{

$this-im = imagecreatefromjpeg($this-srcimg);

}

if($this-type=="gif")

{

$this-im = imagecreatefromgif($this-srcimg);

}

if($this-type=="png")

{

$this-im = imagecreatefrompng($this-srcimg);

}

}

/*

function initi_img($f)

{

//GetImageSize获取图像信息,数组表示,print_r ($data);

$data=GetImageSize($f);

switch($data[2]){

case 1:

$this-im = imagecreatefromgif($f);

break;

case 2:

$this-im = imagecreatefromjpeg($f);

break;

case 3:

$this-im = imagecreatefrompng($f);

break;

}

}

*/

//图象目标地址

function dst_img()

{

$full_length = strlen($this-srcimg);

$type_length = strlen($this-type);

$name_length = $full_length-$type_length;

$name = substr($this-srcimg,0,$name_length-1);

$this-dstimg = $name."_small.".$this-type;

}

}

/*

print_r($_FILES["file"]);

foreach ($_FILES["file"] as $key = $a)

{

echo $key."=============".$a."br";

}

*/

$tempimgname = strtolower($_FILES["file"][name]);

//mb_convert_encoding转换传输字集encod

$tempimgname = mb_convert_encoding( $tempimgname, "gb2312", "utf-8");

//echo $tempimgname;

$tmpfiletype = substr(strrchr($tempimgname,"."),1);

/*

echo $tmpfiletype."----------------"."br";

//explode 以某个字符串分割。

$firsttmpfilename = explode(".",$_FILES["file"][tmp_name]);

echo $firsttmpfilename[0]."br";

$tempimgname = $firsttmpfilename[0].".".$tmpfiletype;

echo $tempimgname."br";

*/

if($tmpfiletype=="jpg" || $tmpfiletype=="gif" || $tmpfiletype=="png")

{

if(copy($_FILES["file"]["tmp_name"],strtolower("image\".date("Y-m-",time()).$tempimgname)))

{

//输出原来的文件名,大小,类型

echo "文件".strtolower($_FILES["file"]["name"])."br"."大小".$_FILES["file"]["size"]."br"."文件类型为".$_FILES["file"]["type"]."br"."文件上传成功!"."br";

}

else

{

echo "文件上传失败"."br";

}

$class = new resizeimage("image\".date("Y-m-",time()).$tempimgname, 120, 160, 1);

}

else

{

echo "上传文件类型错误,请勿上传除jpg,gif,png以外的其他文件。";

}

?

$class = new resizeimage("image\".date("Y-m-",time()).$tempimgname, 120, 160, 1);内120,160为想改变的图像大小。1为截图,0为不截图,具体效果请自行体会。

php 中 图片如何输出多尺寸

php的gd库可以实现读取宽和高

GetImageSize

作用:取得图片的大小[即长与宽]

PHP GD库函用法:array GetImageSize(string filename, array [imageinfo]);

这里我很好奇的问一句为什么要宽和高呢?

如果你是直接输出原样式大小,不用写长或宽,在html页面显示的就是原图大小

如果你是因为页面的关系,直接width=“300”,高度会自动等比变化~

thinkphp 如何生产指定图片尺寸大小的?

这是我项目中的一个thinkphp方法,如果不覆盖原图那修改save中的文件名为新名称就可以。

/* 生成规格图片

 * param:file 操作的图片,完整路径+文件名

 * param:size 缩略图最大尺寸

*/

function make_thumb($file,$width,$height){

    $image = new ThinkImage(); 

    $image-open($file);

    $image-thumb($width, $height)-save($file);

}

PHP图片生成

给你一个php 图像处理类,完全能实现你的功能,你自己研究一下吧

?php

class image

{

var $w_pct = 50; //透明度

var $w_quality = 80; //质量

var $w_minwidth = 300; //最小宽

var $w_minheight = 300; //最小高

var $thumb_enable; //是否生成缩略图

var $watermark_enable; //是否生水印

var $interlace = 0; //图像是否为隔行扫描的

var $fontfile; //字体文件

var $w_img ; //默认水印图

function __construct()

{

global $SITE_CONFING;

$this-thumb_enable = $SITE_CONFING['thumb_enable'];

$this-watermark_enable = $SITE_CONFING['watermark_enable'];

$this-set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);

}

function image()

{

$this-__construct();

}

function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)

{

$this-w_minwidth = $w_minwidth;

$this-w_minheight = $w_minheight;

$this-w_quality = $w_quality;

$this-w_pct = $w_pct;

$this-fontfile = $fontfile;

$this-w_img = $w_img;

}

function info($img)

{

$imageinfo = getimagesize($img); //返回图像信息数组 0=宽的像素 1=高的像素 2=是图像类型的标记 3 =是文本字符串,内容为“height="yyy" width="xxx"”,

if($imageinfo === false) return false;

$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //获取图像文件类型 $imageinfo[2]是图像类型的标记

$imagesize = filesize($img); //图像大小

$info = array(

'width'=$imageinfo[0],

'height'=$imageinfo[1],

'type'=$imagetype,

'size'=$imagesize,

'mime'=$imageinfo['mime']

);

return $info;

}

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0)

{

if(!$this-thumb_enable || !$this-check($image)) return false;

$info = $this-info($image); //获取图片信息

if($info === false) return false;

$srcwidth = $info['width']; //源图宽

$srcheight = $info['height']; //源图高

$pathinfo = pathinfo($image);

$type = $pathinfo['extension']; //取得扩展名

if(!$type) $type = $info['type']; //如果没有取到,用$info['type']

$type = strtolower($type);

unset($info);

$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //获取缩略比例

//获取按照源图的比列

$createwidth = $width = (int)($srcwidth*$scale); //取得缩略宽

$createheight = $height = (int)($srcheight*$scale); //取得缩略高

$psrc_x = $psrc_y = 0;

if($autocut) //按照缩略图的比例来获取

{

if($maxwidth/$maxheight$srcwidth/$srcheight $maxheight=$height) //如果缩略图按比列比源图窄的话

{

$width = $maxheight/$height*$width; //宽按照相应比例做处理

$height = $maxheight; //高不变

}

elseif($maxwidth/$maxheight$srcwidth/$srcheight $maxwidth=$width)//如果缩略图按比列比源图宽的话

{

$height = $maxwidth/$width*$height;

$width = $maxwidth;

}

$createwidth = $maxwidth;

$createheight = $maxheight;

}

$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的图像处理函数

$srcimg = $createfun($image); //新建图像

if($type != 'gif' function_exists('imagecreatetruecolor'))

$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一个真彩色图像

else

$thumbimg = imagecreate($width, $height); //新建一个基于调色板的图像

if(function_exists('imagecopyresampled')) //重采样拷贝部分图像并调整大小,真彩

//imagecopyresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)

imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);

else //拷贝部分图像并调整大小,调色板

imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);

if($type=='gif' || $type=='png')

{

//imagecolorallocate 为一幅图像分配颜色

$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 给基于调色板的图像填充背景色, 指派一个绿色

// imagecolortransparent 将某个颜色定义为透明色

imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图

}

// imageinterlace 激活或禁止隔行扫描

if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this-interlace);

$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);

//imagejpeg imagegif imagepng

if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //获取文件名

//aaa.gif aaa_thumb.gif

$imagefun($thumbimg, $filename); //新建图像

imagedestroy($thumbimg); //销毁缩略图

imagedestroy($srcimg); //销毁源图

return $filename;

}

//watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)

function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')

{

if(!$this-watermark_enable || !$this-check($source)) return false;

if(!$target) $target = $source;

if ($w_img == '' $w_text == '')

$w_img = $this-w_img;

$source_info = getimagesize($source);

$source_w = $source_info[0]; //获取宽

$source_h = $source_info[1]; //获取高

if($source_w $this-w_minwidth || $source_h $this-w_minheight) return false; //宽和高达不到要求直接返回

switch($source_info[2]) //新建图片

{

case 1 :

$source_img = imagecreatefromgif($source);

break;

case 2 :

$source_img = imagecreatefromjpeg($source);

break;

case 3 :

$source_img = imagecreatefrompng($source);

break;

default :

return false;

}

if(!empty($w_img) file_exists($w_img)) //水印文件

{

$ifwaterimage = 1; //是否水印图

$water_info = getimagesize($w_img); //水印信息

$width = $water_info[0];

$height = $water_info[1];

switch($water_info[2])

{

case 1 :

$water_img = imagecreatefromgif($w_img);

break;

case 2 :

$water_img = imagecreatefromjpeg($w_img);

break;

case 3 :

$water_img = imagecreatefrompng($w_img);

break;

default :

return;

}

}

else

{

$ifwaterimage = 0;

//imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。

//imagettfbbox ( 字体大小, 字体角度, 字体文件,文件 )

$temp = imagettfbbox(ceil($w_font*1.2), 0, $this-fontfile, $w_text);//取得使用 truetype 字体的文本的范围

$width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置

$height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置

unset($temp);

}

switch($w_pos)

{

case 0: //随机位置

$wx = rand(0,($source_w - $width));

$wy = rand(0,($source_h - $height));

break;

case 1: //左上角

$wx = 5;

$wy = 5;

break;

case 2: //上面中间位置

$wx = ($source_w - $width) / 2;

$wy = 0;

break;

case 3: //右上角

$wx = $source_w - $width;

$wy = 0;

break;

case 4: //左面中间位置

$wx = 0;

$wy = ($source_h - $height) / 2;

break;

case 5: //中间位置

$wx = ($source_w - $width) / 2;

$wy = ($source_h - $height) / 2;

break;

case 6: //底部中间位置

$wx = ($source_w - $width) / 2;

$wy = $source_h - $height;

break;

case 7: //左下角

$wx = 0;

$wy = $source_h - $height;

break;

case 8: //右面中间位置

$wx = $source_w - $width;

$wy = ($source_h - $height) /2;

break;

case 9: //右下角

$wx = $source_w - $width;

$wy = $source_h - $height ;

break;

default: //随机

$wx = rand(0,($source_w - $width));

$wy = rand(0,($source_h - $height));

break;

}

if($ifwaterimage) //如果有水印图

{

//imagecopymerge 拷贝并合并图像的一部分

//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)

imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this-w_pct);

}

else

{

if(!empty($w_color) (strlen($w_color)==7))

{

$r = hexdec(substr($w_color,1,2)); //获取红色

$g = hexdec(substr($w_color,3,2)); //获取绿色

$b = hexdec(substr($w_color,5)); //获取蓝色

}

else

{

return;

}

//imagecolorallocate 基于调色板的图像填充背景色

//imagestring 水平地画一行字符串

//imagestring(源图,字体大小,位置X,位置Y,文字,颜色)

//参数($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)

imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this-fontfile,$w_text);

//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));

}

//输出到文件或者浏览器

switch($source_info[2])

{

case 1 :

imagegif($source_img, $target); //以 GIF 格式将图像输出到浏览器或文件

break;

case 2 :

imagejpeg($source_img, $target, $this-w_quality); //以 JPEG 格式将图像输出到浏览器或文件

break;

case 3 :

imagepng($source_img, $target); //以 PNG 格式将图像输出到浏览器或文件

break;

default :

return;

}

if(isset($water_info))

{

unset($water_info); //销毁

}

if(isset($water_img))

{

imagedestroy($water_img); //销毁

}

unset($source_info);

imagedestroy($source_img);

return true;

}

//gd库必须存在,后缀为jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在

function check($image)

{

return extension_loaded('gd')

preg_match("/.(jpg|jpeg|gif|png)/i", $image, $m)

file_exists($image)

function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));

//imagecreatefromjpeg

//imagecreatefromgif

//imagecreatefrompng

}

}

/**

缩略图

1.新建一个图像资源 通过 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng

2.imagecopyresampled 拷贝图像,并调整大小

水印:图片水印,文字水印

1. 创建图像

2.加水印

图片水印:imagecopymerge 把2张图合并在一起

文字水印:imagettftext 向图像写入文字

*/

?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。