PHP implements a method to automatically generate thumbnails based on url, url is automatically generated_PHP tutorial

WBOY
Release: 2016-07-13 10:18:36
Original
798 people have browsed it

php implements the method of automatically generating thumbnails based on url, and the url is automatically generated

The example in this article describes how PHP can automatically generate thumbnails based on URLs, which is a very practical function. Share it with everyone for your reference. The specific method is as follows:

Principle: Set apache rewrite, and when the image does not exist, call php to create the image.

For example:

The original image path is: http://localhost/upload/news/2013/07/21/1.jpg
The thumbnail path is: http://localhost/supload/news/2013/07/21/1.jpg

When accessing http://localhost/supload/news/2013/07/21/1.jpg, if the image exists, the image will be displayed. Otherwise, call createthumb.php to generate the image.

The directory structure is as follows:

www/PicThumb.class.php
www/ThumbConfig.php
www/upload/news/2013/07/21/1.jpg
www/upload/article/2013/07/21/2.jpg
www/supload/.htaccess
www/supload/watermark.png
www/supload/createthumb.php

http://localhost/ points to the www directory

Please check here for usage of PicThumb.class.php: http://www.bkjia.com/article/55530.htm

Need to enable apache rewrite:

sudo a2enmod rewrite 

Copy after login

.htaccess file is as follows:

<IfModule mod_rewrite.c> 
RewriteEngine On 
 
# '-s' (is regular file, with size) 
# '-l' (is symbolic link) 
# '-d' (is directory) 
# 'ornext|OR' (or next condition) 
# 'nocase|NC' (no case) 
# 'last|L' (last rule) 
 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^.*$ createthumb.php&#63;path=%{REQUEST_URI} [NC,L] 
 
</IfModule>

Copy after login

The createthumb.php file is as follows:

<&#63;php
define('WWW_PATH', dirname(dirname(__FILE__))); // 站点www目录 
 
require(WWW_PATH.'/PicThumb.class.php'); // include PicThumb.class.php 
require(WWW_PATH.'/ThumbConfig.php');  // include ThumbConfig.php 
 
$logfile = WWW_PATH.'/createthumb.log'; // 日志文件 
$source_path = WWW_PATH.'/upload/';   // 原路径 
$dest_path = WWW_PATH.'/supload/';    // 目标路径 
 
$path = isset($_GET['path'])&#63; $_GET['path'] : ''; // 访问的图片URL 
 
// 检查path 
if(!$path){ 
  exit(); 
} 
 
// 获取图片URI 
$relative_url = str_replace($dest_path, '', WWW_PATH.$path); 
 
// 获取type 
$type = substr($relative_url, 0, strpos($relative_url, '/')); 
 
// 获取config 
$config = isset($thumb_config[$type])&#63; $thumb_config[$type] : ''; 
 
// 检查config 
if(!$config || !isset($config['fromdir'])){ 
  exit(); 
} 
 
// 原图文件 
$source = str_replace('/'.$type.'/', '/'.$config['fromdir'].'/', $source_path.$relative_url); 
 
// 目标文件  
$dest = $dest_path.$relative_url; 
 
// 创建缩略图 
$obj = new PicThumb($logfile); 
$obj->set_config($config); 
if($obj->create_thumb($source, $dest)){ 
  ob_clean(); 
  header('content-type:'.mime_content_type($dest)); 
  exit(file_get_contents($dest)); 
} 
 
&#63;>

Copy after login

ThumbConfig.php file is as follows:

<&#63;php 
 
$thumb_config = array( 
 
  'news' => array( 
    'fromdir' => 'news', // 来源目录 
    'type' => 'fit', 
    'width' => 100, 
    'height' => 100, 
    'bgcolor' => '#FF0000' 
  ), 
 
  'news_1' => array( 
    'fromdir' => 'news', 
    'type' => 'fit', 
    'width' => 200, 
    'height' => 200, 
    'bgcolor' => '#FFFF00' 
  ), 
 
  'article' => array( 
    'fromdir' => 'article', 
    'type' => 'crop', 
    'width' => 250, 
    'height' => 250, 
    'watermark' => WWW_PATH.'/supload/watermark.png' 
  ) 
 
); 
 
&#63;>

Copy after login

After accessing these three paths, thumbnails will be automatically generated according to config
http://localhost/supload/news/2013/07/21/1.jpg
http://localhost/supload/news_1/2013/07/21/1.jpg
http://localhost/supload/article/2013/07/21/2.jpg

Click here to download the complete code of the example described in this article.

I hope this article will be helpful to everyone’s PHP programming design.

url implementation thumbnail

The php environment needs to support the GD library.

$img_name=$_GET['img']; //Get query string

$src_img=imagecreatefromjpeg($img_name);

$ow=imagesx($src_img);
$oh=imagesy($src_img);

$desc_img=imagecreate(400,300);

imagecopyresized($desc_img,$src_img, 0,0,0,0,400,300,$ow,$oh);

imagejpeg($desc_img);

imagedestroy($desc_img);
imagedestroy($src_img);

?>

The above code can generate a 400*300 thumbnail based on an image, such as:

www.xx.com/image.php?img=1.jpg

Requirement 1.jpg must exist and the size is arbitrary. And 1.jpg and image.php are in the same directory.

www.xx.com/image.php?img=upload2009/1.jpg

Also, no need to change, just use the one above. Anyway, the $img_name variable is the URL of the image. You can change it yourself according to the actual situation.

php automatically generates thumbnail code

Let me give you a function
// *****generate thumbnails*****
// Only consider jpg, png, gif formats
// $srcImgPath source image path
// $targetImgPath target image path
// $targetW target image width
// $targetH target image height
function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH )
{
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ Make the function not output error information to the page
switch ($imgType )
{
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
}
//Get the width and height of the source image
$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 checks whether the function has been defined
//ImageCreateTrueColor This function requires GD2 .0.1 or higher version
if(function...the rest of the text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/882893.htmlTechArticlephp method to automatically generate thumbnails based on url, url is automatically generated. This article describes the example of php automatically generating based on url. The thumbnail method is a very practical function. Share it with everyone...
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