按比例生成缩略图代码-生成图片

 更新时间:2016年11月25日 16:58  点击:1492

<?php教程
 
class My_Lib_simpleimage {
 var $image;
 var $image_type;
 function load($filename) { 
    //$filename 文件的临时文件名 $_FILES['name']['tmp_name']
  $image_info = getimagesize($filename);
  $this->image_type = $image_info[2];
  if( $this->image_type == IMAGETYPE_JPEG ) {
  $this->image = imagecreatefromjpeg($filename);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {
  $this->image = imagecreatefromgif($filename);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {
  $this->image = imagecreatefrompng($filename);
  }
 }
 
 function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  if( $image_type == IMAGETYPE_JPEG ) {
  imagejpeg($this->image,$filename,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {
  imagegif($this->image,$filename);
  } elseif( $image_type == IMAGETYPE_PNG ) {
  imagepng($this->image,$filename);
  }
  if( $permissions != null) {
  chmod($filename,$permissions);
  }
 }
 function output($image_type=IMAGETYPE_JPEG) {
  if( $image_type == IMAGETYPE_JPEG ) {
  imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {
  imagegif($this->image);
  } elseif( $image_type == IMAGETYPE_PNG ) {
  imagepng($this->image);
  }
 }
 function getWidth() {
  return imagesx($this->image);
 }
 function getHeight() {
  return imagesy($this->image);
 }
 
 #设置缩略图片高度
 function resizeToHeight($height) {
  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
 }
 #设置缩略图片宽度
 function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
 }
 function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100;
  $this->resize($width,$height);
 }
 function resize($width,$height) {
  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
 }
}
?>

function image3($length=4,$type='png',$width=180,$height=60,$fontface='fpnf.ttf',$verifyName='verify') {
        $code = $this->rand_string($length,4);
        $width = ($length*25)>$width?$length*25:$width;
        $authCode = new Zend_Session_Namespace('Auth_Code');
        $authCode->imagecode = $randval;
       
        $im=imagecreatetruecolor($width,$height);
        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //边框色
        $bkcolor=imagecolorallocate($im,250,250,250);
        imagefill($im,0,0,$bkcolor);
        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
        // 干扰
        for($i=0;$i<15;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
        }
        for($i=0;$i<255;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
        }
        if(!is_file($fontface)) {
            $fontface = dirname(__FILE__)."/".$fontface;
        }
        for($i=0;$i<$length;$i++){
            $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
            $codex= substr($code,$i,1);
            imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
        }
        $this->output($im,$type);
    }
   
    function output($im,$type='png')
    {
        header("Content-type: image/".$type);
        $ImageFun='Image'.$type;
        $ImageFun($im);
        imagedestroy($im);
    }

php imagecreatefromgif创建 gif图片

imagecreatefromgif($filename)
imagecreatefromgif()返回一个图像标识符代表从给定的文件名获得的图像

$filename是gif图片名称哦。

来看个实例

<?php
function LoadGif($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefromgif($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a blank image */
        $im = imagecreatetruecolor (150, 30);
        $bgc = imagecolorallocate ($im, 255, 255, 255);
        $tc = imagecolorallocate ($im, 0, 0, 0);

        imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring ($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

header('Content-Type: image/gif');

$img = LoadGif('bogus.image');

imagegif($img);
imagedestroy($img);
?>

php imagecreate实例教程

描述
资源imagecreate($width,$height)
imagecreate()返回一个图像标识符代表了指定大小的空白图像。

我们建议使用imagecreatetruecolor()。

看个
<?php
header("Content-type: image/png");
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

如果您可以访问gd.h(很少在一个共享的主机),您可以修改GD_RESOLUTION并给它所需的价值。

该解决方案,我发现直接操纵的JPEG头字节。
我希望此示例代码将帮助别人,因为我花了5挖掘推测离开系统I小时,没有其他选择。

<?php
header('Content-Disposition: attachment; filename="myimg.jpg"');
header('Cache-Control: private');
$dst    = imagecreatetruecolor(300, 300); /*this creates a 300x300 pixels image*/
ob_start(); /*don't send the output to the browser since we'll need to manipulate it*/
ImageJpeg($dst);
$img = ob_get_contents();
ob_end_clean();

//byte #14 in jpeg sets the resolution type: 0=none, 1=pixels per inch, 2=pixels per cm
//bytes 15-16 sets X resolution
//bytes 17-18 sets Y resolution
$img = substr_replace($img, pack('cnn',1,300,300),13,5);

echo $img;

?>

文件在其文件类型,并返回假的负载如果失败
<?php
function imagecreatefromfile($path, $user_functions = false)
{
    $info = @getimagesize($path);
   
    if(!$info)
    {
        return false;
    }
   
    $functions = array(
        IMAGETYPE_GIF => 'imagecreatefromgif',
        IMAGETYPE_JPEG => 'imagecreatefromjpeg',
        IMAGETYPE_PNG => 'imagecreatefrompng',
        IMAGETYPE_WBMP => 'imagecreatefromwbmp',
        IMAGETYPE_XBM => 'imagecreatefromwxbm',
        );
   
    if($user_functions)
    {
        $functions[IMAGETYPE_BMP] = 'imagecreatefrombmp';
    }
   
    if(!$functions[$info[2]])
    {
        return false;
    }
   
    if(!function_exists($functions[$info[2]]))
    {
        return false;
    }
   
    return $functions[$info[2]]($path);
}
?>

php 验证码生成程序[可自动判断php gd库]
function ShowKey()
{
$key=strtolower(domake_password(4));
$set=esetcookie("checkkey",$key);
 //是否支持gd库
if (function_exists("imagejpeg")) {
   header ("Content-type: image/jpeg");
   $img=imagecreate(69,20);
   $black=imagecolorallocate($img,255,255,255);
   $gray=imagecolorallocate($img,102,102,102);
   imagefill($img,0,0,$gray);
   imagestring($img,3,14,3,$key,$black);
   imagejpeg($img);
   imagedestroy($img);
}
elseif (function_exists("imagegif")) {
   header ("Content-type: image/gif");
   $img=imagecreate(69,20);
   $black=imagecolorallocate($img,255,255,255);
   $gray=imagecolorallocate($img,102,102,102);
   imagefill($img,0,0,$gray);
   imagestring($img,3,14,3,$key,$black);
   imagegif($img);
   imagedestroy($img);
}
elseif (function_exists("imagepng")) {
 header ("Content-type: image/png");
   $img=imagecreate(69,20);
   $black=imagecolorallocate($img,255,255,255);
   $gray=imagecolorallocate($img,102,102,102);
   imagefill($img,0,0,$gray);
   imagestring($img,3,14,3,$key,$black);
   imagepng($img);
   imagedestroy($img);
}
elseif (function_exists("imagewbmp")) {
 header ("Content-type: image/vnd.wap.wbmp");
   $img=imagecreate(69,20);
   $black=imagecolorallocate($img,255,255,255);
   $gray=imagecolorallocate($img,102,102,102);
   imagefill($img,0,0,$gray);
   imagestring($img,3,14,3,$key,$black);
   imagewbmp($img);
   imagedestroy($img);
}
else {
 $set=esetcookie("checkkey","ebak");
 @include("class/functions.php");
 echo ReadFiletext("images/ebak.jpg");
}
}
ShowKey();

[!--infotagslink--]

相关文章

  • c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
  • PHP批量生成图片缩略图(1/5)

    这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
  • C#实现为一张大尺寸图片创建缩略图的方法

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

    if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
  • .net生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法

    这篇文章主要介绍了.net生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法 ,需要的朋友可以参考下...2021-09-22
  • PHP实例:EmailAddress生成图片程序

    把下面的文件保存为index.php <? /* MailX Managment System 0.8 Beta */ header("Content-type:image/png"); $mailaddress=$_GET['mailname']; $mailaddres...2016-11-25
  • CentOS下编译安装nginx及配置缩略图插件的方法教程

    这篇文章主要给大家介绍了在CentOS系统下编译安装nginx及配置缩略图插件的方法教程,文中给出了详细的安装步骤,对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。...2017-07-06
  • c#多图片上传并生成缩略图的实例代码

    今天写了一个上传多张图片并生成缩略图的小程序。当然因为是菜鸟,所以写的一般。但还是学到了不少东西。现在上代码。...2021-09-22
  • Nginx配合php实现生成实时缩略图功能

    这篇文章主要介绍了Nginx配合php实现生成实时缩略图功能,这在一些特殊场合可能会要用到,需要的朋友可以参考下...2016-01-27
  • php 图片上传代码(具有生成缩略图与增加水印功能)

    这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。 代码如下 复制代码 ...2016-11-25
  • C# jpg缩略图函数代码

    生成jpg缩略图字节,本人的小软件中需要用到的功能,所以自己做了一个函数,和大家分享 为什么要生成字节而不是文件,这是为了方便后续处理啦^_^...2020-06-25
  • js通过canvas生成图片缩略图

    对于生成缩略图一般做法是通过后端语言php等来生成,但是为了给服务器减压,我们或许可以从前端来着手,先生成好不同尺寸的缩略图,传给后端,而后端只需要将前端传过来的图片进行存储就好了...2020-10-03
  • php支持生成缩略图文件上传代码

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1...2016-11-25
  • php生成图片文字混合图片的例子

    生成图片文字混合图片通常用于验证码了,下面来看一个关于php生成图片文字混合图片的例子,希望对大家会有所帮助. 例子一 图片合成文字 <?php $im = imagecreatet...2016-11-25
  • C#获取视频某一帧的缩略图的方法

    这篇文章主要介绍了C#获取视频某一帧的缩略图的方法,涉及执行CMD命令及针对视频文件操作的技巧,具有一定的实用价值,需要的朋友可以参考下...2020-06-25
  • php利用Imagick把pdf生成png缩略图

    缩略图如果是图片我们直接使用php gD库就可实现了,本文章要介绍的是Imagick把pdf生成png缩略图方法,这里我们要利用一个插件了,下面我来给大家演示一个实例。 php_im...2016-11-25
  • php图片上传类同时可生成缩略图与加水印

    这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印。 代码如下 复制代码 class upimages { var $annexf...2016-11-25
  • ASP.NET创建动态缩略图的方法

    这篇文章主要介绍了ASP.NET创建动态缩略图的方法,实例分析了asp.net动态操作图片的相关技巧,需要的朋友可以参考下...2021-09-22
  • php文件上传程序(支持缩略图)(1/2)

    代码如下 复制代码 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional....2016-11-25
  • phpStudy2013中php无法生成图片解决办法

    在php中生成图片是需要用到一个GD库的,默认情况下phpStudy都是打开了,但到了phpStudy2013之后GD库就像php默认安装一个不再开启这个功能了,要用户需配置一下。 phpSt...2016-11-25