×

php缩略图代码

php缩略图代码(php缩小图片)

admin admin 发表于2023-03-25 05:50:10 浏览44 评论0

抢沙发发表评论

本文目录一览:

php关于显示缩略图的编码问题

?php echo "img src=\"slt.php\""; ?

你是要把slt.php当图片用?

PHP文章图片缩略图怎么实现可以点击进入文章链接,wordpress怎么实现可以点击缩略图进入文章链接

我的站是模版自带了程序了,所以不用我费心去研究这个缩略图的问题,但我的另一个站就没有自带了,文章列表页全是文字,没有显示缩略图的,这对我企业站来说,客人浏览产品很不方便,于是打算自己动手研究一下,如何调用第一张图片为缩略图的方法。-php缩略图代码

经网上一查,发现有一种方法很好用,而且不用插件,现在介绍给大家。

1、在模板函数(functions.php)中插入以下代码:

function catch_that_image() {

global $post, $posts;

$first_img = ”;

ob_start();

ob_end_clean();

$output = preg_match_all(‘/img.+src=[\'"]([^\'"]+)[\'"].*/i’, $post-post_content, $matches);

$first_img = $matches [1] [0];

if(empty($first_img)){ //Defines a default image

$first_img = “”;

}

return $first_img;

}

2、在HTML代码中,即你需要调用第一张缩略图的地方加入以下代码:

?php $rand_posts =

get_posts(‘numberposts=50000category=1,5,6,9,10,11,18,28,29,30,31,32orderby=date’);foreach($rand_posts-php缩略图代码

as $post) : ?

a href=”?php the_permalink();

?”img src=”?php echo catch_that_image() ?”

width=”160″ height=”120″//a

category=1,5,6,9,10,11,18,28,29,30,31,32 是指分类目录,你需要调用哪个分类目录文章的缩略图,就写上相对应的分类目录ID号。这样,就自动调用出来了指定分类目录的第一张图片为缩略图了。-php缩略图代码

width=”160″ height=”120″ 是指调用出来后的缩略图大小。

最后,祝大家玩WORDPRESS愉快。

php创建缩略图问题

其实PHP创建缩略图就是在PHP在原图片的基础上创建一张新的图片的过程,而用PHP创建图像的过程一般分成四部:

第一步:创建一张画布(只要是画图都需要一张画布的)

第二步:在画布画东西(可以画各种图形,如长方形,直线,等等,也可以在画布上写字啥的,或者画其他的图形)

第三步:画完图之后,将图片输出,将图片输出到浏览器,在浏览器显示出来,或者保存为一张新 的图片(缩略图一般是保存为图片文件的)

第四步:因为创建画布时打开了文件流,所以要关闭资源,节省内存。(个人觉得你可以这样理解,打开了一画布,把它铺开了,画完了就把画布卷起来,收起来,不要占着铺的地方)

具体的代码如下:(这段代码来源于ThinkPHP的图像类)

?php

class Thumb{

   /**

     * @param string $image  原图

     * @param string $thumbname 缩略图文件名

     * @param string $type 图像格式

     * @param string $maxWidth  宽度

     * @param string $maxHeight  高度

   */

   static create($img, $thumbname, $type='', $maxWidth=200, $maxHeight=50)

   {

       $info = getimagesize($img);    //获取原图的图像信息(长、宽、格式等)

       if ($info !== false) {

            $srcWidth = $info['width'];

            $srcHeight = $info['height'];

            $type = empty($type) ? $info['type'] : $type;

            $type = strtolower($type);

            $interlace = $interlace ? 1 : 0;

            unset($info);

            $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例

            if ($scale = 1) {

                // 超过原图大小不再缩略

                $width = $srcWidth;

                $height = $srcHeight;

            } else {

                // 缩略图尺寸

                $width = (int) ($srcWidth * $scale);

                $height = (int) ($srcHeight * $scale);

            }

            // 载入原图(在原图的基础上创建画布,为第一步)

            $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);

            if(!function_exists($createFun)) {

                return false;

            }

            $srcImg = $createFun($image);

            //第二步开始

            //创建缩略图

            if ($type != 'gif'  function_exists('imagecreatetruecolor'))

                $thumbImg = imagecreatetruecolor($width, $height);

            else

                $thumbImg = imagecreate($width, $height);

              //png和gif的透明处理 by luofei614

            if('png'==$type){

                imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题)

                imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)    

            }elseif('gif'==$type){

                $trnprt_indx = imagecolortransparent($srcImg);

                 if ($trnprt_indx = 0) {

                        //its transparent

                       $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);

                       $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);-php缩略图代码

                       imagefill($thumbImg, 0, 0, $trnprt_indx);

                       imagecolortransparent($thumbImg, $trnprt_indx);

              }

            }

            // 复制图片

            if (function_exists("ImageCopyResampled"))

                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);-php缩略图代码

            else

                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);-php缩略图代码

           //第三步:输出图像

            // 生成图片

            $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);

            $imageFun($thumbImg, $thumbname);

            

            //第四步:关闭画布

            imagedestroy($thumbImg);

            imagedestroy($srcImg);

            return $thumbname;

        }

        return false;

       

   }

}

?

你使用的时候直接用:

require Thumb.class.php

$thumb = Thumb::create('s.jpg','thumb_s.jpg',100,50);

希望我的回答你能满意

能直接用的PHP生成缩略图的程序(要求简单)

?php

/*构造函数-生成缩略图+水印,参数说明:

$srcFile-图片文件名,

$dstFile-另存文件名,

$markwords-水印文字,

$markimage-水印图片,

$dstW-图片保存宽度,

$dstH-图片保存高度,

$rate-图片保存品质*/

makethumb("a.jpg","b.jpg","50","50");

function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$markwords=null,$markimage=null)

{

$data = GetImageSize($srcFile);

switch($data[2])

{

case 1:

$im=@ImageCreateFromGIF($srcFile);

break;

case 2:

$im=@ImageCreateFromJPEG($srcFile);

break;

case 3:

$im=@ImageCreateFromPNG($srcFile);

break;

}

if(!$im) return False;

$srcW=ImageSX($im);

$srcH=ImageSY($im);

$dstX=0;

$dstY=0;

if ($srcW*$dstH$srcH*$dstW)

{

$fdstH = round($srcH*$dstW/$srcW);

$dstY = floor(($dstH-$fdstH)/2);

$fdstW = $dstW;

}

else

{

$fdstW = round($srcW*$dstH/$srcH);

$dstX = floor(($dstW-$fdstW)/2);

$fdstH = $dstH;

}

$ni=ImageCreateTrueColor($dstW,$dstH);

$dstX=($dstX0)?0:$dstX;

$dstY=($dstX0)?0:$dstY;

$dstX=($dstX($dstW/2))?floor($dstW/2):$dstX;

$dstY=($dstY($dstH/2))?floor($dstH/s):$dstY;

$white = ImageColorAllocate($ni,255,255,255);

$black = ImageColorAllocate($ni,0,0,0);

imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色

ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);

if($markwords!=null)

{

$markwords=iconv("gb2312","UTF-8",$markwords);

//转换文字编码

ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //写入文字水印

//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容

}

elseif($markimage!=null)

{

$wimage_data = GetImageSize($markimage);

switch($wimage_data[2])

{

case 1:

$wimage=@ImageCreateFromGIF($markimage);

break;

case 2:

$wimage=@ImageCreateFromJPEG($markimage);

break;

case 3:

$wimage=@ImageCreateFromPNG($markimage);

break;

}

imagecopy($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31

imagedestroy($wimage);

}

ImageJpeg($ni,$dstFile,$rate);

ImageJpeg($ni,$srcFile,$rate);

imagedestroy($im);

imagedestroy($ni);

}

?

php怎么生成缩略图

给你个函数吧

 // *****生成缩略图*****

     // 只考虑jpg,png,gif格式

     // $srcImgPath 源图象路径

     // $targetImgPath 目标图象路径

     // $targetW 目标图象宽度

     // $targetH 目标图象高度

     function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)

     {

         $imgSize = GetImageSize($srcImgPath);

         $imgType = $imgSize[2];

         //@ 使函数不向页面输出错误信息

         switch ($imgType)

        {

            case 1:

                $srcImg = @ImageCreateFromGIF($srcImgPath);

                break;

            case 2:

                $srcImg = @ImageCreateFromJpeg($srcImgPath);

                break;

            case 3:

                $srcImg = @ImageCreateFromPNG($srcImgPath);

                break;

        }

         //取源图象的宽高

        $srcW = ImageSX($srcImg);

        $srcH = ImageSY($srcImg);

        if($srcW$targetW || $srcH$targetH)

        {

            $targetX = 0;

            $targetY = 0;

            if ($srcW  $srcH)

            {

                $finaW=$targetW;

                $finalH=round($srcH*$finaW/$srcW);

                $targetY=floor(($targetH-$finalH)/2);

            }

            else

            {

                $finalH=$targetH;

                $finaW=round($srcW*$finalH/$srcH);

                $targetX=floor(($targetW-$finaW)/2);

            }

              //function_exists 检查函数是否已定义

              //ImageCreateTrueColor 本函数需要GD2.0.1或更高版本

            if(function_exists("ImageCreateTrueColor"))

            {

                $targetImg=ImageCreateTrueColor($targetW,$targetH);

            }

            else

              {

                $targetImg=ImageCreate($targetW,$targetH);

            }

            $targetX=($targetX0)?0:$targetX;

            $targetY=($targetX0)?0:$targetY;

            $targetX=($targetX($targetW/2))?floor($targetW/2):$targetX;

            $targetY=($targetY($targetH/2))?floor($targetH/2):$targetY;

              //背景白色

            $white = ImageColorAllocate($targetImg, 255,255,255);

            ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);

            /*

                   PHP的GD扩展提供了两个函数来缩放图象:

                   ImageCopyResized 在所有GD版本中有效,其缩放图象的算法比较粗糙,可能会导致图象边缘的锯齿。

                   ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值算法得到的图象边缘比较平滑,

                                                             该函数的速度比ImageCopyResized慢。

            */

            if(function_exists("ImageCopyResampled"))

            {

                ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);-php缩略图代码

            }

            else

            {

                ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);-php缩略图代码

            }

              switch ($imgType) {

                case 1:

                    ImageGIF($targetImg,$targetImgPath);

                    break;

                case 2:

                    ImageJpeg($targetImg,$targetImgPath);

                    break;

                case 3:

                    ImagePNG($targetImg,$targetImgPath);

                    break;

            }

            ImageDestroy($srcImg);

            ImageDestroy($targetImg);

        }

         else //不超出指定宽高则直接复制

        {

            copy($srcImgPath,$targetImgPath);

            ImageDestroy($srcImg);

        }

     }

代码已经测试,成功运行!

PHP 图片上传生成缩略图

//2014年3月5日15:08:02 因为需要做缩略图,所以改用thinkphp来做上传,它支持时间戳命名,方便命名,以及更名

//这是以前百度到的,然后使用的缩略图代码,需要cg库支持

    /**

     * 生成缩略图

     * @author yangzhiguo0903@163.com

     * @param string     源图绝对完整地址{带文件名及后缀名}

     * @param string     目标图绝对完整地址{带文件名及后缀名}

     * @param int        缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}

     * @param int        缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}

     * @param int        是否裁切{宽,高必须非0}

     * @param int/float  缩放{0:不缩放, 0this1:缩放到相应比例(此时宽高限制和裁切均失效)}

     * @return boolean

     */

    function fileext($file)

    {

        return strtolower(pathinfo($file, PATHINFO_EXTENSION));

    }

    function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)

    {

        if(!is_file($src_img))

        {

            return false;

        }

        $ot = $this-fileext($dst_img);

        $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);

        $srcinfo = getimagesize($src_img);

        $src_w = $srcinfo[0];

        $src_h = $srcinfo[1];

        $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));

        $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

     

        $dst_h = $height;

        $dst_w = $width;

        $x = $y = 0;

     

        /**

         * 缩略图不超过源图尺寸(前提是宽或高只有一个)

         */

        if(($width $src_w  $height $src_h) || ($height $src_h  $width == 0) || ($width $src_w  $height == 0))-php缩略图代码

        {

            $proportion = 1;

        }

        if($width $src_w)

        {

            $dst_w = $width = $src_w;

        }

        if($height $src_h)

        {

            $dst_h = $height = $src_h;

        }

     

        if(!$width  !$height  !$proportion)

        {

            return false;

        }

        if(!$proportion)

        {

            if($cut == 0)

            {

                if($dst_w  $dst_h)

                {

                    if($dst_w/$src_w $dst_h/$src_h)

                    {

                        $dst_w = $src_w * ($dst_h / $src_h);

                        $x = 0 - ($dst_w - $width) / 2;

                    }

                    else

                    {

                        $dst_h = $src_h * ($dst_w / $src_w);

                        $y = 0 - ($dst_h - $height) / 2;

                    }

                }

              else if($dst_w xor $dst_h)

                {

                    if($dst_w  !$dst_h)  //有宽无高

                    {

                        $propor = $dst_w / $src_w;

                        $height = $dst_h  = $src_h * $propor;

                    }

                    else if(!$dst_w  $dst_h)  //有高无宽

                    {

                        $propor = $dst_h / $src_h;

                        $width  = $dst_w = $src_w * $propor;

                    }

                }

            }

            else

            {

                if(!$dst_h)  //裁剪时无高

                {

                    $height = $dst_h = $dst_w;

                }

                if(!$dst_w)  //裁剪时无宽

                {

                    $width = $dst_w = $dst_h;

                }

                $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);

                $dst_w = (int)round($src_w * $propor);

                $dst_h = (int)round($src_h * $propor);

                $x = ($width - $dst_w) / 2;

                $y = ($height - $dst_h) / 2;

            }

        }

        else

        {

            $proportion = min($proportion, 1);

            $height = $dst_h = $src_h * $proportion;

            $width  = $dst_w = $src_w * $proportion;

        }

     

        $src = $createfun($src_img);

        $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);

        $white = imagecolorallocate($dst, 255, 255, 255);

        imagefill($dst, 0, 0, $white);

     

        if(function_exists('imagecopyresampled'))

        {

            imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);

        }

        else

        {

            imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);

        }

        $otfunc($dst, $dst_img);

        imagedestroy($dst);

        imagedestroy($src);

        return true;

    }