二款php 图片等比例缩放

 更新时间:2016年11月25日 16:58  点击:1316
php 图片等比例缩放代码是一款根据用户上传的图片来指定比例大小的图片,原理很简单就是算出图片大小进等比例就行了,第二款生成小图是固定图片大小,但是如何图片小于设定的图片就填白,这是一个好方法哦。
 代码如下 复制代码

<?php
header("content-type:image/jpeg");
$filename = hsdir.'/mljntc2p.jpg';
$im = imagecreatefromjpeg($filename);
$h=imagesy($im);//获得目标图片高度
$new_img_width  = 257;
$new_img_height = 522;

$newim = imagecreate($new_img_width, $new_img_height);
$white = imagecolorallocate($newim, 255,255,255); //设置背景颜色
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_img_width, $new_img_height, $new_img_width, $new_img_height);
imagefilledrectangle($newim,0,$h,$new_img_width,$new_img_height,$white);
//填充  目标图片高度作为起驶y坐标 以指定截取宽高为结束坐标
imagejpeg($newim);
imagedestroy($newim);
imagedestroy($im);
?>

代码二

 代码如下 复制代码

<?php
header("content-type:image/jpeg");
$filename = 'myface.jpg';
$im = imagecreatefromjpeg($filename);
$new_img_width  = 80;
$new_img_height = 150;
$newim = imagecreate($new_img_width, $new_img_height);
$white = imagecolorallocate($newim, 255,255,255); //设置背景颜色
imagecopyresized($newim, $im, 0, 0, 0, 0, $new_img_width, $new_img_height, $new_img_width, $new_img_height);
imagejpeg($newim);
imagedestroy($newim);
imagedestroy($im);
?>

本文章里面image就是你要生成的图片地址哦,这是一php按比例生成缩略图代码,只要给你图片他就能生成指定大小的图片哦,并且不变形ekt
 代码如下 复制代码

function resizeimage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imagetype) = getimagesize($image);
 $imagetype = image_type_to_mime_type($imagetype);
 $newimagewidth = ceil($width * $scale);
 $newimageheight = ceil($height * $scale);
 $newimage = imagecreatetruecolor($newimagewidth,$newimageheight);
 switch($imagetype) {
  case "image/gif":
   $source=imagecreatefromgif($image);
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image);
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image);
   break;
   }
 imagecopyresampled($newimage,$source,0,0,0,0,$newimagewidth,$newimageheight,$width,$height);
 
 switch($imagetype) {
  case "image/gif":
     imagegif($newimage,$image);
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newimage,$image,90);
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newimage,$image); 
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
?>

这款关于php 中文汉字验证码生成程序其实很简单的,生成中文汉字验证码需要字体来支持了,下面我们用的arial.ttf来支持生成,否则生成中文图片验证码会出乱码的。
 代码如下 复制代码

class securecode
{
    private static $instance=null;
    private $code = '';
    private $fontfile;
    private $validate;
    private $image;
    private $specialadd = 'special string for securecode';
    private $codeexpire=86400;
    private $codecookiename='secure_code';

    /**
     * 构造方法
     */
    private function securecode()
    {
        $this->fontfile = dirname( __file__ ) . '/arial.ttf';
    }

    private function __construct()
    {
        $this->securecode();
    }
   
    public static function getinstance()
    {
        if (self::$instance==null)
            self::$instance=new self();
       
        return self::$instance;
    }

    /**
     * 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
     * @param $fontfile 文件路径
     * @return void
     */
    function loadfont($fontfile)
    {
        $this->fontfile = $fontfile;
    }

    /**
     * 图片输出方法,在执行本方法前程序不应该有任何形式的输出
     * @return void;
     */
    function stroke()
    {
        $this->savecode();
        self::sendheader();
        imagegif( $this->validate );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }

    /**
     * 图片保存方法
     * @param $filename 保存路径
     * @return void
     */
    function save($filename)
    {
        $this->savecode();
        imagegif( $this->validate , $filename );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }
   
  /**
     * 验证码验证方法
     * @param $input 要验证的字符串,即用户的输入内容
     * @return boolean 验证结果
     */
    function verify($input)
    {
        $input=strtolower($input);
        $targetcode=$this->authcode($input);
        $code=$this->getcookie();
        if (empty($code)||$code!=$targetcode)
            $result= false;
        else
            $result=true;
        $_cookie[$this->codecookiename]='';
        setcookie ( $this->codecookiename, '', - 1 );
        return $result;
    }

    /**
     * 图片创建方法
     * @return void;
     */
    function createimage()
    {
        $this->randcode();
       
        $size = 30;
        $width = 90;
        $height = 35;
        $degrees = array (
            rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
        );
       

        for ($i = 0; $i < 4; ++$i)
        {
            if (rand() % 2);
            else $degrees[$i] = -$degrees[$i];
        }
       
        $this->image = imagecreatetruecolor( $size , $size );
        $this->validate = imagecreatetruecolor( $width , $height );
        $back = imagecolorallocate( $this->image , 255 , 255 , 255 );
        $border = imagecolorallocate( $this->image , 0 , 0 , 0 );
        imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
       
        for ($i = 0; $i < 4; ++$i)
        {
            $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
           
            if ($temp[2] > 60) $temp[2] = 60;
           
            $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
            $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
        }
       
        for ($i = 0; $i < 200; ++$i)
        {
            $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
            imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
        }
       
        $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
       
        if ($temp[2] < 200) $temp[2] = 255;
       
        $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
        $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
       
        self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
       
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        putenv( 'gdfontpath=' . realpath( '.' ) );
       
        // name the font to be used (note the lack of the .ttf extension
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
       
        $this->image = imagerotate( $this->image , $degrees[0] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
        $this->image = imagerotate( $this->image , $degrees[1] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
        $this->image = imagerotate( $this->image , $degrees[2] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
        $this->image = imagerotate( $this->image , $degrees[3] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
        imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
    }

 

在很多地方我们都会看到把ip地址生成在图片哦,今天我们就来告诉你如何利用php 把ip生成在图片上哦,好了下面我们就来看看实例吧。

 代码如下 复制代码

$img = imagecreate(180,50);

  $ip = $_server['remote_addr'];

  imagecolortransparent($img,$bgcolor);

  $bgcolor = imagecolorallocate($img, 0x2c,0x6d,0xaf); // 背景颜色

  $shadow = imagecolorallocate($img, 250,0,0); // 阴影颜色

  $textcolor = imagecolorallocate($img, oxff,oxff,oxff); // 字体颜色

  imagettftext($img,10,0,78,30,$shadow,"c:/windows/fonts/tahoma.ttf",$ip);

  //显示背景

  imagettftext($img,10,0,25,28,$textcolor,"c:/windows/fonts/tahoma.ttf","your ip is".$ip);

  // 显示ip

  imagepng($img);

  imagecreatefrompng($img);

  imagedestroy($img);

其实就利用了php生成图片功能,生成验证码也经常会用到这样的功能哦。

本文章免费提供一款php图片处理类:缩略,裁剪,圆角,倾斜代码,他可以切出不同风格的图片哦,哈哈,比其它的在线切图要好很多了哦。
 代码如下 复制代码
class resizeimage
{
   //图片类型
   var $type;
   //实际宽度
   var $width;
   //实际高度
   var $height;
   //改变后的宽度
   var $resize_width;
   //改变后的高度
   var $resize_height;
   //是否裁图
   var $cut;
   //源图象
   var $srcimg;
   //目标图象地址
   var $dstimg;
   //圆角源
   var $corner;
   var $im;

function resizeimage($img, $corner, $wid, $hei,$c, $corner_radius, $angle)
   {
       $this->srcimg = $img;
           $this->corner = $corner;
       $this->resize_width = $wid;
       $this->resize_height = $hei;
       $this->cut = $c;
           $this->corner_radius = $corner_radius;
           $this->angle = $angle;
       //图片的类型
       $this->type = substr(strrchr($this->srcimg,"."),1);
       //初始化图象
       $this->initi_img();
       //目标图象地址
       $this -> dst_img();
       //--
       $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);
                           $tmp = $this->rounded_corner($newimg,$this->resize_width);
               imagepng ($tmp,$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));
                           $tmp = $this->rounded_corner($newimg);
               imagepng ($tmp,$this->dstimg);
           }
       }
       else
      

 

 

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • C#将图片和字节流互相转换并显示到页面上

    本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25
  • JavaScript 如何禁止用户保存图片

    这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19
  • php上传图片学习笔记与心得

    我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
  • SwiftUI图片缩放、拼图等处理教程

    SwiftUI是一种使用Swift语言在苹果设备上构建用户界面的创新且简单的方式,下面这篇文章主要给大家介绍了关于SwiftUI图片缩放、拼图等处理的相关资料,需要的朋友可以参考下...2021-08-23