PHP图片上传功能,传一张图片自动生成一个大的图片一个小的缩略图

q652820916 2010-03-28 04:08:22
PHP图片上传功能,传一张图片自动生成一个大的图片一个小的缩略图
...全文
329 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
an9ryfr09 2010-03-29
  • 打赏
  • 举报
回复
估计又是求作业的,现在的大学生啊,不会写您好歹也得学会用google去搜啊。
mu_rain 2010-03-29
  • 打赏
  • 举报
回复
呵呵,我这有存库。

//缩放图片
/**
* *
*等比缩放
* @param unknown_type $srcImage 源图片路径
* @param unknown_type $toFile 目标图片路径
* @param unknown_type $maxWidth 最大宽
* @param unknown_type $maxHeight 最大高
* @param unknown_type $imgQuality 图片质量
* @return unknown
*/

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{

list($width, $height, $type, $attr) = getimagesize($srcImage);
if($width < $maxWidth || $height < $maxHeight) return ;
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例

if($scale < 1) {
$newWidth = floor($scale*$width);
$newHeight = floor($scale*$height);
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
$toFile = preg_replace("/(\.gif|\.jpg|\.jpeg|\.png)/i","",$toFile);

switch($type) {
case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}


//直接压制指定大小,不进行等比处理
function resize_2($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{

list($width, $height, $type, $attr) = getimagesize($srcImage);
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例


$newWidth = $maxWidth;
$newHeight = $maxHeight;
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$newName = "";
switch($type) {
case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
return "$newName.jpg"; break;

imagedestroy($newImg);
}
imagedestroy($img);
return false;

}
kyzy_yy_pm 2010-03-29
  • 打赏
  • 举报
回复
<?php
/**
* File: Thumb.php
* Project:图片的缩略、水印
* CreateTime:2009-12-2
* @author quanzhijie
*/

error_reporting(E_ERROR);

/**
*创建图Constant.php文件(可选)
**
*-水印文字--
*@define FONT_SIZE 字的大小
*@define ANGLE 水印的角度
*@define X 水印的x轴位置
*@define Y 水印的y轴位置
*@define COL_R 水印颜色R值
*@define COL_G 水印颜色G值
*@define COL_B 水印颜色B值
*@define FONT 字体
*@define TEXT 水印的内容
*-水印图片--
*@define IMG 水印图标
*@define COPY_AT_X 水印图标的x轴位置
*@define COPY_AT_Y 水印图标的y轴位置
*@define COPY_TO_X 水印图标在背景图的x轴位置
*@define COPY_TO_Y 水印图标在背景图的y轴位置
*@define COPY_TO_W 水印图标的宽
*@define COPY_TO_H 水印图标的高
*/
$filename = 'Constant.php';
if(file_exists($filename))
include_once('Constant.php');

class Common_Thumb{

/**
*创建图片水印
*@param $bgfile: 背景图
*@param $fgfile: 待拷贝的图
*@param $save 生成文件保存地址
*@param $copy_at_X: 拷贝到背景图上的X坐标
*@param $copy_at_Y: 拷贝到背景图上的Y坐标
*@param $copy_to_X: 水印图标在背景图的x轴位置
*@param $copy_to_Y: 水印图标在背景图的y轴位置
*@param $copyToWidth: 水印图标的宽
*@param $copyToHeight:水印图标的高
*/
function watermarkIMG($bgfile){

$newimg = $bgfile;
$fgfile = defined('IMG')?IMG:'';
$copy_at_X = defined('COPY_AT_X')?COPY_AT_X:10;
$copy_at_Y = defined('COPY_AT_Y')?COPY_AT_Y:10;
$copy_to_X = defined('COPY_TO_X')?COPY_TO_X:10;
$copy_to_Y = defined('COPY_TO_Y')?COPY_TO_Y:10;
$copyToWidth = defined('COPY_TO_W')?COPY_TO_W:100;
$copyToHeight = defined('COPY_TO_H')?COPY_TO_H:100;

$bgfile = ImageCreateFromJpeg($bgfile);
$fgfile = ImageCreateFrompng($fgfile);
ImageAlphaBlending($bgfile, true);
$iWidth = ImageSX($fgfile);
$iHeight = ImageSY($fgfile);
ImageCopy($bgfile, $fgfile, $copy_to_X, $copy_to_Y, $copy_at_X, $copy_at_Y, $copyToWidth, $copyToHeight);
ImageJPEG($bgfile, $newimg);
return $newimg;
}

/**
*创建文本水印(注意:此方法中有很多参数是常量)
*@param $oldimg 源图片
*/
function watermarkText($oldimg){

$font = (defined('FONT') && is_file(FONT))?FONT:(is_file('default.ttf')?'default.ttf':print('No font files, watermark content can not be can not be displayed'));
$font_size = defined('FONT_SIZE')?FONT_SIZE:20;
$angle = defined('ANGLE')?ANGLE:45;
$text = defined('TEXT')?TEXT:'CHUSHAN';
$col_r = defined('COL_R')?COL_R:252;
$col_g = defined('COL_G')?COL_G:118;
$col_b = defined('COL_B')?COL_B:0;

list($w, $h) = getimagesize($oldimg);
$x = $w/5;
$y = $h-$h/10;
$img = imageCreateFromJPEG($oldimg);
$grey = imagecolorallocate($img, 255, 255, 255); //为$img分配颜色
$color = ImageColorAt($img, $x, $y) ^ 0xFFFFFF; //取得某像素的颜色索引值
$color = imagecolorallocate($img, $col_r, $col_g, $col_b); //为$img分配颜色
imagettftext($img, $font_size, $angle, $x, $y, $color, $font, $text);
imagejpeg($img,$oldimg);
}

/**
*取得缩略图(调用函数)
*@param $oldimg 源图片
*@param $w 设置宽
*@param $h 设置高
*/
function getThumb($oldimg, $w, $h){

$i1 = strrpos($oldimg, '/');
$i2 = strrpos($oldimg, '.');
$strl2 = strlen(substr($oldimg, $i2));
$newimg = substr($oldimg, 0, $i1).'/thumb/'.substr($oldimg, $i1+1, $i2-$i1-1).'_'.$w.'_'.$h.substr($oldimg, $i2);

if(!is_file($newimg)){
$newimg = $this->createThumb($oldimg, $newimg, $w, $h);
}
$data = explode('/', $newimg);
$len = count($data);
$result = $data[$len-3].'/'.$data[$len-2].'/'.$data[$len-1];
return $result;
}
/**
*创建缩略图
*@param $oldimg 源图片
*@param $newimg 生成的图片
*@param $w 生成图片的宽
*@param $h 生成图片的高
*/
function createThumb($oldimg, $newimg, $w, $h, $b){

list($width, $height, $type) = getimagesize($oldimg);
if($width<=$w && $height<=$h) {
$p = 1;
}else{
if($width < $height){
$p = $h/$height;
}else{
$p = $w/$width;
}
}
$h = $height*$p;
$w = $width*$p;

$i1 = strrpos($newimg, '/');
$newdir = substr($newimg, 0, $i1);
if(!is_dir($newdir)) {
@mkdir($newdir, 0777);
}

$img = ($type==1)?imagecreatefromgif($oldimg):(($type==2)?imagecreatefromjpeg($oldimg):imagecreatefrompng($oldimg));
$thumb = imagecreatetruecolor($w, $h);
if(function_exists("imagecreatetruecolor"))
{
ImageCopyResampled($thumb, $img, 0, 0, 0, 0, $w, $h, $width , $height);
} else {
ImageCopyResized($thumb, $img, 0, 0, 0, 0, $w, $h, $width , $height);
}
imagejpeg($thumb, $newimg);
($type==1)? imagegif ($thumb, $newimg): (($type==2)? imagejpeg($thumb, $newimg): imagepng($thumb, $newimg));
imagedestroy($img);
imagedestroy($thumb);

return $newimg;
}
}
?>
uuq 2010-03-29
  • 打赏
  • 举报
回复
看我的小框架
可以自动生成一张小图片


queryphp框架发布新版 自动生成html 添加分页类 图片处理类
http://topic.csdn.net/u/20100329/14/889b2109-214e-4a20-8f85-c7c0b2e9e053.html?26020
ivanlw 2010-03-28
  • 打赏
  • 举报
回复
你这个是求助还是要教别人呢?
cooledit2730 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 q652820916 的回复:]
PHP图片上传功能,传一张图片自动生成一个大的图片一个小的缩略图
[/Quote]

你为什么不直接传一张大的图片只生成小的图片呢?
joesen_li 2010-03-28
  • 打赏
  • 举报
回复
哥真的8会咧~
sciolist 2010-03-28
  • 打赏
  • 举报
回复


$f = new SaeFetchurl();

$img_data = $f->fetch( 'http://ss7.sinaimg.cn/bmiddle/488efcbbt7b5c4ae51ca6&690' );

$img = new SaeImage();

$img->setData( $img_data );

$img->resize(200); // 等比缩放到200宽

$img->flipH(); // 水平翻转

$img->flipv(); // 垂直翻转

$new_data = $img->exec(); // 执行处理并返回处理后的二进制数据

// 或者可以直接输出

$img->exec( 'jpg' , true );

PIGer920 2010-03-28
  • 打赏
  • 举报
回复
可以利用swfupload。
如果是想用php对图片进行尺寸修改可以用keyword"imagecreatetruecolor"在google检索一下

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧