php生成缩略图类,支持自定义高和宽。还支持按高和宽截图

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

php教程生成缩略图类,支持自定义高和宽。还支持按高和宽截图

<?php 
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,$dstpath) 
    { 
        $this->srcimg = $img; 
        $this->resize_width = $wid; 
        $this->resize_height = $hei; 
        $this->cut = $c; 
        //图片的类型 

$this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
        //初始化图象 
        $this->initi_img(); 
        //目标图象地址 
        $this -> dst_img($dstpath); 
        //-- 
        $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(($this->cut)=="1") 
        //裁图 
        { 
            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->height)*$resize_ratio), $this->height); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            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)); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
        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); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
            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); 
                ImageJpeg ($newimg,$this->dstimg); 
            } 
        } 
    } 
    //初始化图象 
    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 dst_img($dstpath) 
    { 
        $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 = $dstpath; 

//echo $this->dstimg; 
    } 

$resizeimage = new resizeimage("11.jpg", "200", "150", "1","17.jpg"); 

php教程上传图片后,自动裁剪成缩略图,宽不限高

<?php
// $Id: image.php 1937 2009-01-05 19:09:40Z dualface $

/**
* 定义 Helper_Image 类和 Helper_ImageGD 类
*
* @link http://qeephp.com/
* @copyright Copyright (c) 2006-2009 Qeeyuan Inc. {@link http://www.qeeyuan.com}
* @license New BSD License {@link http://qeephp.com/license/}
* @version $Id: image.php 1937 2009-01-05 19:09:40Z dualface $
* @package helper
*/

/**
* Helper_Image 类封装了针对图像的操作
*
* 开发者不能直接构造该类的实例,而是应该用 Helper_Image::createFromFile()
* 静态方法创建一个 Image 类的实例。
*
* 操作大图片时,请确保 php 能够分配足够的内存。
*
* @author YuLei Liao <liaoyulei@qeeyuan.com>
* @version $Id: image.php 1937 2009-01-05 19:09:40Z dualface $
* @package helper
*/
abstract class Helper_Image
{
    /**
     * 从指定文件创建 Helper_ImageGD 对象
     *
     * 用法:
     * @code php
     * $image = Helper_Image::createFromFile('1.jpg');
     * $image->resize($width, $height);
     * $image->saveAsJpeg('2.jpg');
     * @endcode
     *
     * 对于上传的文件,由于其临时文件名中并没有包含扩展名。
     * 因此需要采用下面的方法创建 Image 对象:
     *
     * @code php
     * $ext = pathinfo($_FILES['postfile']['name'], PATHINFO_EXTENSION);
     * $image = Image::createFromFile($_FILES['postfile']['tmp_name'], $ext);
     * @endcode
     *
     * @param string $filename 图像文件的完整路径
     * @param string $fileext 指定扩展名
     *
     * @return Helper_ImageGD 从文件创建的 Helper_ImageGD 对象
     * @throw Q_NotImplementedException
     */
    static function createFromFile($filename, $fileext)
    {
        $fileext = trim(strtolower($fileext), '.');
        $ext2functions = array(
            'jpg'  => 'imagecreatefromjpeg',
            'jpeg' => 'imagecreatefromjpeg',
            'png'  => 'imagecreatefrompng',
            'gif'  => 'imagecreatefromgif'
        );

        if (!isset($ext2functions[$fileext]))
        {
                throw new Q_NotImplementedException(__('imagecreateform' . $fileext));
        }

        $handle = call_user_func($ext2functions[$fileext], $filename);
        return new Helper_ImageGD($handle);
    }

        /**
         * 将 16 进制颜色值转换为 rgb 值
     *
     * 用法:
     * @code php
     * $color = '#369';
     * list($r, $g, $b) = Helper_Image::hex2rgb($color);
     * echo "red: {$r}, green: {$g}, blue: {$b}";
     * @endcode
     *
     * @param string $color 颜色值
     * @param string $default 使用无效颜色值时返回的默认颜色
         *
         * @return array 由 RGB 三色组成的数组
         */
        static function hex2rgb($color, $default = 'ffffff')
        {
        $hex = trim($color, '#&Hh');
        $len = strlen($hex);
        if ($len == 3)
        {
            $hex = "{$hex[0]}{$hex[0]}{$hex[1]}{$hex[1]}{$hex[2]}{$hex[2]}";
        }
        elseif ($len < 6)
        {
            $hex = $default;
        }
        $dec = hexdec($hex);
        return array(($dec >> 16) & 0xff, ($dec >> 8) & 0xff, $dec & 0xff);
        }
}

/**
* Helper_ImageGD 类封装了一个 gd 句柄,用于对图像进行操作
*
* @author YuLei Liao <liaoyulei@qeeyuan.com>
* @version $Id: image.php 1937 2009-01-05 19:09:40Z dualface $
* @package helper
*/
class Helper_ImageGD
{
    /**
     * GD 资源句柄
     *
     * @var resource
     */
    protected $_handle = null;

    /**
     * 构造函数
     *
     * @param resource $handle GD 资源句柄
     */
    function __construct($handle)
    {
        $this->_handle = $handle;
    }

    /**
     * 析构函数
     */
    function __destruct()
    {
            $this->destroy();
    }

    /**
     * 快速缩放图像到指定大小(质量较差)
     *
     * @param int $width 新的宽度
     * @param int $height 新的高度
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function resize($width, $height)
    {
        if (is_null($this->_handle)) return $this;

        $dest = imagecreatetruecolor($width, $height);
        imagecopyresized($dest, $this->_handle, 0, 0, 0, 0,
            $width, $height,
            imagesx($this->_handle), imagesy($this->_handle));
        imagedestroy($this->_handle);
        $this->_handle = $dest;
        return $this;
    }

    /**
     * 缩放图像到指定大小(质量较好,速度比 resize() 慢)
     *
     * @param int $width 新的宽度
     * @param int $height 新的高度
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function resampled($width, $height)
    {
        if (is_null($this->_handle)) return $this;
        $dest = imagecreatetruecolor($width, $height);
        imagecopyresampled($dest, $this->_handle, 0, 0, 0, 0,
            $width, $height,
            imagesx($this->_handle), imagesy($this->_handle));
        imagedestroy($this->_handle);
        $this->_handle = $dest;
        return $this;
    }

    /**
     * 调整图像大小,但不进行缩放操作
     *
     * 用法:
     * @code php
     * $image->resizeCanvas($width, $height, 'top-left');
     * @endcode
     *
     * $pos 参数指定了调整图像大小时,图像内容按照什么位置对齐。
     * $pos 参数的可用值有:
     *
     * -   left: 左对齐
     * -   right: 右对齐
     * -   center: 中心对齐
     * -   top: 顶部对齐
     * -   bottom: 底部对齐
     * -   top-left, left-top: 左上角对齐
     * -   top-right, right-top: 右上角对齐
     * -   bottom-left, left-bottom: 左下角对齐
     * -   bottom-right, right-bottom: 右下角对齐
     *
     * 如果指定了无效的 $pos 参数,则等同于指定 center。
     *
     * @param int $width 新的高度
     * @param int $height 新的宽度
     * @param string $pos 调整时图像位置的变化
     * @param string $bgcolor 空白部分的默认颜色
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function resizeCanvas($width, $height, $pos = 'center', $bgcolor = '0xffffff')
    {
        if (is_null($this->_handle)) return $this;

        $dest = imagecreatetruecolor($width, $height);
        $sx = imagesx($this->_handle);
        $sy = imagesy($this->_handle);

        // 根据 pos 属性来决定如何定位原始图片
        switch (strtolower($pos))
        {
        case 'left':
            $ox = 0;
            $oy = ($height - $sy) / 2;
            break;
        case 'right':
            $ox = $width - $sx;
            $oy = ($height - $sy) / 2;
            break;
        case 'top':
            $ox = ($width - $sx) / 2;
            $oy = 0;
            break;
        case 'bottom':
            $ox = ($width - $sx) / 2;
            $oy = $height - $sy;
            break;
        case 'top-left':
        case 'left-top':
            $ox = $oy = 0;
            break;
        case 'top-right':
        case 'right-top':
            $ox = $width - $sx;
            $oy = 0;
            break;
        case 'bottom-left':
        case 'left-bottom':
            $ox = 0;
            $oy = $height - $sy;
            break;
        case 'bottom-right':
        case 'right-bottom':
            $ox = $width - $sx;
            $oy = $height - $sy;
            break;
        default:
            $ox = ($width - $sx) / 2;
            $oy = ($height - $sy) / 2;
        }

        list ($r, $g, $b) = Helper_Image::hex2rgb($bgcolor, '0xffffff');
        $bgcolor = imagecolorallocate($dest, $r, $g, $b);
        imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor);
        imagecolordeallocate($dest, $bgcolor);

        imagecopy($dest, $this->_handle, $ox, $oy, 0, 0, $sx, $sy);
        imagedestroy($this->_handle);
        $this->_handle = $dest;

        return $this;
    }

    /**
     * 在保持图像长宽比的情况下将图像裁减到指定大小
     *
     * crop() 在缩放图像时,可以保持图像的长宽比,从而保证图像不会拉高或压扁。
     *
     * crop() 默认情况下会按照 $width 和 $height 参数计算出最大缩放比例,
     * 保持裁减后的图像能够最大程度的充满图片。
     *
     * 例如源图的大小是 800 x 600,而指定的 $width 和 $height 是 200 和 100。
     * 那么源图会被首先缩小为 200 x 150 尺寸,然后裁减掉多余的 50 像素高度。
     *
     * 用法:
     * @code php
     * $image->crop($width, $height);
     * @endcode
     *
     * 如果希望最终生成图片始终包含完整图像内容,那么应该指定 $options 参数。
     * 该参数可用值有:
     *
     * -   fullimage: 是否保持完整图像
     * -   pos: 缩放时的对齐方式
     * -   bgcolor: 缩放时多余部分的背景色
     * -   enlarge: 是否允许放大
     * -   reduce: 是否允许缩小
     *
     * 其中 $options['pos'] 参数的可用值有:
     *
     * -   left: 左对齐
     * -   right: 右对齐
     * -   center: 中心对齐
     * -   top: 顶部对齐
     * -   bottom: 底部对齐
     * -   top-left, left-top: 左上角对齐
     * -   top-right, right-top: 右上角对齐
     * -   bottom-left, left-bottom: 左下角对齐
     * -   bottom-right, right-bottom: 右下角对齐
     *
     * 如果指定了无效的 $pos 参数,则等同于指定 center。
     *
     * $options 中的每一个选项都可以单独指定,例如在允许裁减的情况下将图像放到新图片的右下角。
     *
     * @code php
     * $image->crop($width, $height, array('pos' => 'right-bottom'));
     * @endcode
     *
     * @param int $width 新的宽度
     * @param int $height 新的高度
     * @param array $options 裁减选项
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function crop($width, $height, $options = array())
    {
        if (is_null($this->_handle)) return $this;

        $default_options = array(
            'fullimage' => false,
            'pos'       => 'center',
            'bgcolor'   => '0xfff',
            'enlarge'   => false,
            'reduce'    => true,
        );
        $options = array_merge($default_options, $options);

        // 创建目标图像
        $dest = imagecreatetruecolor($width, $height);
        // 填充背景色
        list ($r, $g, $b) = Helper_Image::hex2rgb($options['bgcolor'], '0xffffff');
        $bgcolor = imagecolorallocate($dest, $r, $g, $b);
        imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor);
        imagecolordeallocate($dest, $bgcolor);

        // 根据源图计算长宽比
        $full_w = imagesx($this->_handle);
        $full_h = imagesy($this->_handle);
        $ratio_w = doubleval($width) / doubleval($full_w);
        $ratio_h = doubleval($height) / doubleval($full_h);

        if ($options['fullimage'])
        {
            // 如果要保持完整图像,则选择最小的比率
            $ratio = $ratio_w < $ratio_h ? $ratio_w : $ratio_h;
        }
        else
        {
            // 否则选择最大的比率
            $ratio = $ratio_w > $ratio_h ? $ratio_w : $ratio_h;
        }

        if (!$options['enlarge'] && $ratio > 1) $ratio = 1;
        if (!$options['reduce'] && $ratio < 1) $ratio = 1;

        // 计算目标区域的宽高、位置
        $dst_w = $full_w * $ratio;
        $dst_h = $full_h * $ratio;

        // 根据 pos 属性来决定如何定位
        switch (strtolower($options['pos']))
        {
        case 'left':
            $dst_x = 0;
            $dst_y = ($height - $dst_h) / 2;
            break;
        case 'right':
            $dst_x = $width - $dst_w;
            $dst_y = ($height - $dst_h) / 2;
            break;
        case 'top':
            $dst_x = ($width - $dst_w) / 2;
            $dst_y = 0;
            break;
        case 'bottom':
            $dst_x = ($width - $dst_w) / 2;
            $dst_y = $height - $dst_h;
            break;
        case 'top-left':
        case 'left-top':
            $dst_x = $dst_y = 0;
            break;
        case 'top-right':
        case 'right-top':
            $dst_x = $width - $dst_w;
            $dst_y = 0;
            break;
        case 'bottom-left':
        case 'left-bottom':
            $dst_x = 0;
            $dst_y = $height - $dst_h;
            break;
        case 'bottom-right':
        case 'right-bottom':
            $dst_x = $width - $dst_w;
            $dst_y = $height - $dst_h;
            break;
        case 'center':
        default:
            $dst_x = ($width - $dst_w) / 2;
            $dst_y = ($height - $dst_h) / 2;
        }

        imagecopyresampled($dest,  $this->_handle, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $full_w, $full_h);
        imagedestroy($this->_handle);
        $this->_handle = $dest;

        return $this;
    }

    /**
     * 保存为 JPEG 文件
     *
     * @param string $filename 保存文件名
     * @param int $quality 品质参数,默认为 80
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function saveAsJpeg($filename, $quality = 80)
    {
        imagejpeg($this->_handle, $filename, $quality);
    }

    /**
     * 保存为 PNG 文件
     *
     * @param string $filename 保存文件名
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function saveAsPng($filename)
    {
        imagepng($this->_handle, $filename);
    }

    /**
     * 保存为 GIF 文件
     *
     * @param string $filename 保存文件名
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function saveAsGif($filename)
    {
        imagegif($this->_handle, $filename);
    }

    /**
     * 销毁内存中的图像
     *
     * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口
     */
    function destroy()
    {
            if (!$this->_handle)
            {
            @imagedestroy($this->_handle);
            }
        $this->_handle = null;
        return $this;
    }
}

调用方法

<?php
$image = Helper_Image::createFromFile('c:a.jpg','jpg');
$image->resampled(100, 100);  //缩放到100px * 100PX
$image->saveAsJpeg('c:a_output.jpg', 100);

imagecreatetruecolor()创建一个真彩色图像后,
并不会自动把imagecolorallocate()方法注册的第一个颜色作为背景色,而必须用imagefill()去填充


//设置颜色

$bg = imagecolorallocate($im, 240, 240, 0);//设置背景颜色
imagefill($im,0,0,$bg);//载入背景颜色
$te = imagecolorallocate($im, 0, 0, 0);//字符串颜色
//将字符串加到图片上
imagestring($im,rand(3,6),rand(5,60),rand(5,15),$rand,$te);
//输出图片
header("Content-type: image/jpeg");
imagejpeg($im);

imagecreatetruecolor()返回一个图像标识符代表指定大小的黑色形象。

根据你的PHP和GD版本中函数定义与否。对于PHP 4.0.6通过4.1.x这个函数总是存在的


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

此代码可以为上传的图片加鲨鱼同时还可以比例缩放图片,有需要的朋友可以参考一下。

<?php教程
$uptypes=array('image/jpg', //上传文件类型列表
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png');
$max_file_size=5000000; //上传文件大小限制, 单位BYTE
$destination_folder="upload/"; //上传文件路径
$watermark=1; //是否附加水印(1为加水印,其他为不加水印);
$watertype=1; //水印类型(1为文字,2为图片)
$waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="newphp.site.cz"; //水印字符串
$waterimg="xplore.gif"; //水印图片
$imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2; //缩略图比例
?>
<html>
<head>
<title>M4U BLOG - fywyj.cn</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css教程">body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}
strong{font-size:12px;}
aink{color:#0066CC;}
a:hover{color:#FF6600;}
aisited{color:#003366;}
a:active{color:#9DCC00;}
table.itable{}
td.irows{height:20px;background:url("index.php?i=dots" repeat-x bottom}</style>
</head>
<body>
<center><form enctype="multipart/form-data" method="post" name="upform">
上传文件: <br><br><br>
<input name="upfile" type="file" style="width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17">
<input type="submit" value="上传" style="width:30;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17"><br><br><br>
允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf <br><br>
<a href="index.php">返回</a>
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "<font color='red'>文件不存在!</font>";
exit;
}

$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "<font color='red'>文件太大!</font>";
exit;
}

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "<font color='red'>只能上传图像文件或Flash!</font>";
exit;
}

if(!file_exists($destination_folder))
mkdir($destination_folder);

$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
echo "<font color='red'>同名文件已经存在了!</a>";
exit;
}

if(!move_uploaded_file ($filename, $destination))
{
echo "<font color='red'>移动文件出错!</a>";
exit;
}

$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
echo " 宽度:".$image_size[0];
echo " 长度:".$image_size[1];
if($watermark==1)
{
$iinfo=getimagesize($destination,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($destination);
break;
case 2:
$simage =imagecreatefromjpeg($destination);
break;
case 3:
$simage =imagecreatefrompng($destination);
break;
case 6:
$simage =imagecreatefromwbmp($destination);
break;
default:
die("<font color='red'>不能上传此类型文件!</a>");
exit;
}

imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);

switch($watertype)
{
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印图片
$simage1 =imagecreatefromgif("xplore.gif");
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}

switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case 2:
imagejpeg($nimage, $destination);
break;
case 3:
imagepng($nimage, $destination);
break;
case 6:
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}

//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}

if($imgpreview==1)
{
echo "<br>图片预览:<br>";
echo "<a href="".$destination."" target='_blank'><img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
echo " alt="图片预览:r文件名:".$destination."r上传时间:" border='0'></a>";
}
}
?>
</center>
</body>
</html>

可以教你个最简单的办法,对于JPG格式的图片,看最后两个字节是否是FFD9


如果不是的话,那么一般是不正常的。如果是其它格式的话,这个判断方式就不适用了。
正常的JPG文件都是以FFD8开头,FFD9结尾的,如果丢失了文件尾部,JPG仍然可以被识别,但是就会丢失部分图像数据。

--------
当然,我说的这种只是简单的判断,可能丢失前的那段结尾的正好是FFD9,但显然还是不正确的,但基本够用。如果追求严谨,那么就得阅读JPEG的格式规范文档了,
这已经不属于PHP的知识范畴了。

我试过判断图片格式和像素
你这显然不行,不够深入,要严谨,就必须深入了解JPG的存储。
至于php教程的GD库是否有相应的判断函数,我没关注过,你可以google,我就不多说了。欢迎你研究出来后共享

分享一个读取jpg的php文件

$adress="IMG_XXX.JPG";
$exif = read_exif_data ($adress);
while(list($k,$v)=each($exif)) {
if($k=="Thumbnail"){
$fp=fopen ("/www/home/image/Thumbnail$adress",
'a');
fwrite ($fp, $v);
fclose ($fp);
echo "<br />n";
echo "n";
echo "<br />n";
}else{
echo "$k: $v<br />n";
}
}

[!--infotagslink--]

相关文章

  • C#创建自定义控件及添加自定义属性和事件使用实例详解

    这篇文章主要给大家介绍了关于C#创建自定义控件及添加自定义属性和事件使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • android自定义动态设置Button样式【很常用】

    为了增强android应用的用户体验,我们可以在一些Button按钮上自定义动态的设置一些样式,比如交互时改变字体、颜色、背景图等。 今天来看一个通过重写Button来动态实...2016-09-20
  • Android自定义WebView网络视频播放控件例子

    下面我们来看一篇关于Android自定义WebView网络视频播放控件开发例子,这个文章写得非常的不错下面给各位共享一下吧。 因为业务需要,以下代码均以Youtube网站在线视...2016-10-02
  • 自定义jquery模态窗口插件无法在顶层窗口显示问题

    自定义一个jquery模态窗口插件,将它集成到现有平台框架中时,它只能在mainFrame窗口中显示,无法在顶层窗口显示. 解决这个问题的办法: 通过以下代码就可能实现在顶层窗口弹窗 复制代码 代码如下: $(window.top.documen...2014-05-31
  • 自定义feignClient的常见坑及解决

    这篇文章主要介绍了自定义feignClient的常见坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-20
  • pytorch 自定义卷积核进行卷积操作方式

    今天小编就为大家分享一篇pytorch 自定义卷积核进行卷积操作方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-05-06
  • PHP YII框架开发小技巧之模型(models)中rules自定义验证规则

    YII的models中的rules部分是一些表单的验证规则,对于表单验证十分有用,在相应的视图(views)里面添加了表单,在表单被提交之前程序都会自动先来这里面的规则里验证,只有通过对其有效的限制规则后才能被提交,可以很有效地保证...2015-11-24
  • jquery自定义插件开发之window的实现过程

    这篇文章主要介绍了jquery自定义插件开发之window的实现过程的相关资料,需要的朋友可以参考下...2016-05-09
  • C#自定义事件监听实现方法

    这篇文章主要介绍了C#自定义事件监听实现方法,涉及C#事件监听的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
  • 使用BindingResult 自定义错误信息

    这篇文章主要介绍了使用BindingResult 自定义错误信息,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-23
  • 在Vue中获取自定义属性方法:data-id的实例

    这篇文章主要介绍了在Vue中获取自定义属性方法:data-id的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-09
  • Vue 组件复用多次自定义参数操作

    这篇文章主要介绍了Vue 组件复用多次自定义参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-27
  • pytorch 自定义参数不更新方式

    今天小编就为大家分享一篇pytorch 自定义参数不更新方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-29
  • thinkphp自定义权限管理之名称判断方法

    下面小编就为大家带来一篇thinkphp自定义权限管理之名称判断方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • PHP批量生成图片缩略图(1/5)

    这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
  • Nginx自定义访问日志的配置方式

    Nginx日志主要分为两种:访问日志和错误日志。访问日志主要记录客户端访问Nginx的每一个请求,格式可以自定义。下面这篇文章主要给大家介绍了Nginx自定义访问日志的配置方式,需要的朋友可以参考学习,下面来一起看看吧。...2017-07-06
  • php中header自定义404状态错误页面

    404页面就是一个告诉搜索引擎这个页面不存在了,同时也提示用户可以选择其它的操作了,下面我来给没有apache操作权限朋友介绍php中自定义404页面的操作方法。 方法一...2016-11-25
  • C#实现为一张大尺寸图片创建缩略图的方法

    这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25