喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
 26  1/3  1  2  3  > 
     
标题: 发个缩略图,自己定义图的长宽,不会变形  
 
cooant
高级会员
Rank: 4
被特殊照顾的VIP会员


UID 25785
精华 0
积分 567
帖子 553
金钱 567 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2004-1-16
来自 中华人民共和国
状态 离线
发个缩略图,自己定义图的长宽,不会变形

不会变形,但是比例不要太夸张了。

<?php
class resizeimage
{
    
//图片类型
    
var $type;
    
//实际宽度
    
var $width;
    
//实际高度
    
var $height;
    
//改变后的宽度
    
var $resize_width;
    
//改变后的高度
    
var $resize_height;
    
//源图象
    
var $srcimg;
    
//目标图象地址
    
var $dstimg;
    
//临????建的图象
    
var $im;

    function 
resizeimage($img$wid$hei)
    {
        
$this->srcimg $img;
        
$this->resize_width $wid;
        
$this->resize_height $hei;
        
//图片的类型
        
$this->type substr(strrchr($this->srcimg,"."),1);
        
//初始化图象
        
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);
        }
        
//目标图象地址
        
$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;
        
//--
        
$this->width imagesx($this->im);
        
$this->height imagesy($this->im);
        
//生成图象
        
$this->newimg();
        
ImageDestroy ($this->im);
    }
    function 
newimg()
    {
        
//改变后的图象的比例
        
$resize_ratio = ($this->resize_width)/($this->resize_height);
        
//实际图象的比例
        
$ratio = ($this->width)/($this->height);
        
//实际图象的比例大于改变后的图象的比例
        
if($ratio>=$resize_ratio)
        
//高度优先
        
{
            
$newimg imagecreatetruecolor($this->resize_width,$this->resize_height);
            
imagecopyresampled($newimg$this->im0000$this->resize_width$this->resize_height, (($this->height)*$resize_ratio), $this->height);
            
ImageJpeg ($newimg,$this->dstimg);
        }
        if(
$ratio<$resize_ratio)
        
//宽度优先
        
{
            
$newimg imagecreatetruecolor($this->resize_width,$this->resize_height);
            
imagecopyresampled($newimg$this->im0000$this->resize_width$this->resize_height$this->width, (($this->width)/$resize_ratio));
            
ImageJpeg ($newimg,$this->dstimg);
        }
    }
}
?>




精品PHP中文手册:
http://www.tripow.com/manual/php/
我的博客
http://www.cooant.com/
2004-11-8 06:24 PM#1
查看资料  访问主页  发短消息  顶部
 
cooant
高级会员
Rank: 4
被特殊照顾的VIP会员


UID 25785
精华 0
积分 567
帖子 553
金钱 567 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2004-1-16
来自 中华人民共和国
状态 离线
[推荐阅读] 如何把一个输出自动复制到剪贴板?

居然没有人顶一下,你们不赶兴趣吗?




精品PHP中文手册:
http://www.tripow.com/manual/php/
我的博客
http://www.cooant.com/
2004-11-8 07:04 PM#2
查看资料  访问主页  发短消息  顶部
 
cooldot9876 (cooldot9876)
注册会员
Rank: 2
中级会员



UID 30304
精华 0
积分 195
帖子 187
金钱 195 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2004-7-24
状态 离线
[推荐阅读] 希望我是第一个送你月饼的人!
哦可,我正找GD的缩略功能,但你这个没有调用,我要的是这样子的
<a href="bigpic.jpg" ><img src="small.jpg"></a>
也就是也显示出小的缩略图,然后链接,点激后显示大的缩略图,你的代码给调用的像我这样要求的例子啊,强烈支持一下下. )
2004-11-9 08:54 AM#3
查看资料  发短消息  QQ . .   顶部
 
kingerq
中级会员
Rank: 3Rank: 3
老会员


UID 19868
精华 0
积分 479
帖子 485
金钱 479 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2003-4-25
来自 浙江温州
状态 离线
[推荐阅读] where ?lsts98!
不错,支持一下。



资料:Kingerq's PHP
2004-11-9 09:02 AM#4
查看资料  访问主页  发短消息  顶部
 
cooant
高级会员
Rank: 4
被特殊照顾的VIP会员


UID 25785
精华 0
积分 567
帖子 553
金钱 567 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2004-1-16
来自 中华人民共和国
状态 离线
[推荐阅读] 各位老大:file函数都有哪些用法?给个实例。


QUOTE:
最初由 cooldot9876 发布
[B]哦可,我正找GD的缩略功能,但你这个没有调用,我要的是这样子的
<a href="bigpic.jpg" ><img src="small.jpg"></a>
也就是也显示出小的缩略图,然后链接,点激后显示大的缩略图,你的代码给调用的像我这样要求的例子啊,强烈支持一下下. ) [/B]
可以啊。不会用吗?




精品PHP中文手册:
http://www.tripow.com/manual/php/
我的博客
http://www.cooant.com/
2004-11-9 09:31 PM#5
查看资料  访问主页  发短消息  顶部
 
netorg (netorg)
金牌会员
Rank: 6Rank: 6
资深会员



UID 25606
精华 0
积分 1243
帖子 1286
金钱 1243 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2004-1-4
状态 离线
[推荐阅读] Php连接,操作mysql的问题
多谢, 已收藏.



巴壁虎:http://www.babihu.com
2004-11-9 09:55 PM#6
查看资料  访问主页  发短消息  顶部
 
netorg (netorg)
金牌会员
Rank: 6Rank: 6
资深会员



UID 25606
精华 0
积分 1243
帖子 1286
金钱 1243 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2004-1-4
状态 离线
[推荐阅读] 55555555哦要疯了
这个类不会对图片进行长与宽按比例收缩, 会作图片的裁片处理.



巴壁虎:http://www.babihu.com
2004-11-9 10:03 PM#7
查看资料  访问主页  发短消息  顶部
 
jxxysong (jxxysong)
高级会员
Rank: 4
高级会员



UID 12761
精华 0
积分 902
帖子 910
金钱 902 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2002-2-10
状态 离线
[推荐阅读] 有没有php学习的qq群?
很不错的东西,顶一下。
2004-11-9 10:57 PM#8
查看资料  访问主页  发短消息  QQ . .   顶部
 
jxxysong (jxxysong)
高级会员
Rank: 4
高级会员



UID 12761
精华 0
积分 902
帖子 910
金钱 902 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2002-2-10
状态 离线
[推荐阅读] unlink 在本地可以删除文件,在服务器上怎么删不掉啊,
虽然成功生成了图片。但为什么会报这么多的错呢?

Warning: Missing argument 1 for resizeimage::resizeimage() in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 21

Warning: Missing argument 2 for resizeimage::resizeimage() in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 21

Warning: Missing argument 3 for resizeimage::resizeimage() in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 21

Warning: imagesx(): supplied argument is not a valid Image resource in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 48

Warning: imagesy(): supplied argument is not a valid Image resource in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 49

Warning: Division by zero in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 57

Warning: Division by zero in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 59

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 64

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 65

Warning: imagejpeg(): supplied argument is not a valid Image resource in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 66

Warning: imagedestroy(): supplied argument is not a valid Image resource in E:\Apache2\htdocs\class_temp\pic\CreateSmallPic.inc.php on line 52
2004-11-9 11:02 PM#9
查看资料  访问主页  发短消息  QQ . .   顶部
 
开花石头 (南冥洗)
版主
Rank: 7Rank: 7Rank: 7
努力把内裤穿在外面


UID 7949
精华 12
积分 25663
帖子 4586
金钱 25543 喜悦币
威望 120
人脉 0
阅读权限 100
注册 2001-4-19
状态 在线
[推荐阅读] 框架之间传值问题?
感叹一下,兄弟的代码写的够整齐的,俺该退休了,发个我写的java的生成所略图代码,希望大家能有用,也是按比例缩放的

<?php
/*
     * @(#)Breviary.java    1.0 04/05/24
     *
     *Copyright 2004 [url]www.myferly.com[/url]
     */

    
package image;
    
    
import java.io.File;
    
import java.io.FileOutputStream;
    
import java.awt.Graphics;
    
import java.awt.Image;
    
import java.awt.image.BufferedImage;
    
    
import com.sun.image.codec.jpeg.JPEGCodec;
    
import com.sun.image.codec.jpeg.JPEGImageEncoder;


    
/**
    * <b>JPG图片缩略图的生成</b><br>
    * @author    stone
    * @version    1.0 04/05/21
    * @since       JDK1.0
    */

      
public class Breviary {


    
/**
    * 源图路径
    */    
    
String sourceFile;
    
    
/**
    * 目标图路径
    */
    
String targetFile;
    
    
/**
    * 目标图宽度
    */
    
int targetWidth;
    
    
/**
    * 目标图高度
    */
    
int targetHeight;
    

    public 
void go() throws Exception{
        
        
//读入文件
        
File _file = new Filethis.sourceFile );
        
        
//构造Image对象
        
Image src javax.imageio.ImageIO.read_file );
        
        
//得到源图宽、高
        
int width=src.getWidth(null); 
        
int height=src.getHeight(null);
        
//得到图像的缩放比率
        
double scaling this.getScalingwidthheight );
        
        
//最后成像的宽、高
        
int w = (int) ( width scaling );
        
int h = (int) ( height scaling );
        
        
BufferedImage tag = new BufferedImagewhBufferedImage.TYPE_INT_RGB );
                            
        
//绘制缩小后的图
        
tag.getGraphics().drawImagesrc00whnull ); 
        
//输出到文件流
        
FileOutputStream out=new FileOutputStreamthis.targetFile );
        
JPEGImageEncoder encoder JPEGCodec.createJPEGEncoderout );
        
encoder.encode(tag);
        
out.close();
        
    }
    
    
    
/**
    * 得到图像缩放比率
    * @param    sourceWidth        源图宽
    * @param    sourceHeight    源图高
    * @return    double            缩放比率
    */
    
    
public double getScalingint sourceWidthint sourceHeight )
    {
        
double widthScaling = (double) this.targetWidth / (double) sourceWidth;
        
double heightScaling = (double) this.targetHeight / (double) sourceHeight;
        
        
double scaling = ( widthScaling heightScaling ) ? widthScaling heightScaling;
                
        return 
scaling;
    }
    
    
    
/**
    * 设置源图路径
    * @param    s            源图路径
    * @return    boolean        操作成功返回true
    */
    
    
public boolean setSourceFileString s )
    {
        
        
this.sourceFile s;
        return 
true;
        
    }
    
    
    
/**
    * 设置目标图路径
    * @param    t            目标图路径
    * @return    boolean        操作成功返回true
    */
    
    
public boolean setTargetFileString t )
    {
        
        
this.targetFile t;
        return 
true;
        
    }    


    
/**
    * 设置目标图宽度
    * @param    t            目标图宽度
    * @return    boolean        操作成功返回true
    */
    
    
public boolean setTargetWidthint t )
    {
        
        
this.targetWidth t;
        return 
true;
        
    }
    
    
    
/**
    * 设置目标图高度
    * @param    t            目标图孤高度
    * @return    boolean        操作成功返回true
    */
    
    
public boolean setTargetHeightint t )
    {
        
        
this.targetHeight t;
        return 
true;
        
    }    
    
}
?>




2004-11-9 11:19 PM#10
查看资料  访问主页  Blog  发短消息  QQ . .   ICQ 状态  顶部
 26  1/3  1  2  3  > 
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.040130 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP