PHP generates the same proportion of thumbnail implementation program_PHP tutorial

WBOY
Release: 2016-07-13 10:46:03
Original
911 people have browsed it

Generating thumbnails in PHP is commonly used in program development. Below I have found a few good implementation programs for PHP to generate thumbnails. Friends in need can use them. I personally tested them and it is absolutely easy to use.

Creating image thumbnails takes a lot of time, this code will help to understand the logic of thumbnails.

The code is as follows Copy code
 代码如下 复制代码

 
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
    $ext = explode(".", $filename);
    $ext = $ext[count($ext)-1];

    if($ext == "jpg" || $ext == "jpeg")
        $im = imagecreatefromjpeg($tmpname);
    elseif($ext == "png")
        $im = imagecreatefrompng($tmpname);
    elseif($ext == "gif")
        $im = imagecreatefromgif($tmpname);

    $x = imagesx($im);
    $y = imagesy($im);

    if($x <= $xmax && $y <= $ymax)
return $im;

if($x >= $y) {
        $newx = $xmax;
        $newy = $newx * $y / $x;
    }
    else {
        $newy = $ymax;
        $newx = $x / $y * $newy;
    }

    $im2 = imagecreatetruecolor($newx, $newy);
    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
    return $im2;
}


/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1]; if($ext == "jpg" || $ext == "jpeg")
          $im = imagecreatefromjpeg($tmpname);
elseif($ext == "png")
          $im = imagecreatefrompng($tmpname);
elseif($ext == "gif")
          $im = imagecreatefromgif($tmpname); $x = imagesx($im);
$y = imagesy($im); if($x <= $xmax && $y <= $ymax)
         return $im;<🎜> <🎜> if($x >= $y) {
          $newx = $xmax;
$newy = $newx * $y / $x;
}
else {
          $newy = $ymax;
$newx = $x / $y * $newy;
} $im2 = imagecreatetruecolor($newx, $newy);
Imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
Return $im2;
}

Example 2

The code is as follows
 代码如下 复制代码

function creat_thumbnail($img,$w,$h,$path)
{
 $org_info = getimagesize($img); //获得图像大小且是通过post传递过来的
 //var_dump($org_info);
 //Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime] => image/png )
 $orig_x = $org_info[0]; //图像宽度
 $orig_y = $org_info[1]; //图像高度
 $orig_type = $org_info[2]; //图片类别即后缀 1 = GIF,2 = JPG,3 = PNG,

 //是真彩色图像
 if (function_exists("imagecreatetruecolor"))
 {
  switch($orig_type)
  {
   //从给定的gif文件名中取得的图像
   case 1  : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif";
   break;
   //从给定的jpeg,jpg文件名中取得的图像
   case 2  : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg";
   break;
   //从给定的png文件名中取得的图像
   case 3  : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng";
   break;
  }
 }
 //如果从给定的文件名可取得的图像
 if(function_exists($_function))
 {
  $orig_image = $_function($img); //从给定的$img文件名中取得的图像
 }
 if (($orig_x / $orig_y) >= (4 / 3)) //如果宽/高 >= 4/3
 {
  $y = round($orig_y / ($orig_x / $w)); //对浮点数进行四舍五入
  $x = $w;
 }
 else //即 高/宽 >= 4/3
 {
  $x = round($orig_x / ($orig_y / $h));
  $y = $h;
 }
 $sm_image = imagecreatetruecolor($x, $y); //创建真彩色图片
 //重采样拷贝部分图像并调整大小
 Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y);
 //imageJPEG($sm_image, '', 80); //在浏览器输出图像
 $tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //缩略图的路径
 $thumbnail = @$_creatImage($sm_image, $tnpath, 80); //生成图片,成功返回true(或1)
 imagedestroy ($sm_image); //销毁图像
 if($thumbnail==true)
 {
  return $tnpath;
 }
}

Copy code
function creat_thumbnail($img,$w,$h,$path)
{
$org_info = getimagesize($img); //Get the image size and it is passed through post
//var_dump($org_info);
//Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime ] => image/png )
$orig_x = $org_info[0]; //Image width
$orig_y = $org_info[1]; //Image height
$orig_type = $org_info[2]; //Picture category is suffix 1 = GIF, 2 = JPG, 3 = PNG,

//It is a true color image
if (function_exists("imagecreatetruecolor"))
{
switch($orig_type)
{
//The image obtained from the given gif file name
Case 1 : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif";
break;
//The image obtained from the given jpeg, jpg file name
Case 2 : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg";
break;
//The image obtained from the given png file name
Case 3 : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng";
break;
}
}
//If the image is available from the given file name
if(function_exists($_function))
{
$orig_image = $_function($img); //The image obtained from the given $img file name
}
if (($orig_x / $orig_y) >= (4 / 3)) //If width/height >= 4/3
{
$y = round($orig_y / ($orig_x / $w)); //Round floating point numbers
$x = $w;
}
else //i.e. height/width >= 4/3
{
$x = round($orig_x / ($orig_y / $h));
$y = $h;
}
$sm_image = imagecreatetruecolor($x, $y); //Create a true color image
//Resample copy part of the image and resize
Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y);
//imageJPEG($sm_image, '', 80); //Output the image in the browser
$tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //Thumbnail path
$thumbnail = @$_creatImage($sm_image, $tnpath, 80); //Generate image, return true (or 1) successfully
imagedestroy ($sm_image); //Destroy image
if($thumbnail==true)
{
return $tnpath;
}
}

http://www.bkjia.com/PHPjc/632962.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/632962.html
TechArticleGenerating thumbnails in php is commonly used in program development. Below I have found a few good php generated thumbnails. The implementation program of the sketch can be used by friends who need it. I personally tested it and it is absolutely easy to use. Create...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template