This article discusses how to generate thumbnails in PHP. We are using PHP to generate the thumbnail HTML code! DOCTYPEHTMLPUBLIC-W3CDTDHTML4.0TransitionalENHTMLHEADTITLE Upload image TITLEMETANAMEGeneratorCONTENT we are using
HTML code for generating thumbnails in PHP
- <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
- < HTML>
- < HEAD>
- < TITLE>Upload images</TITLE>
- < METANAME = "Generator" CONTENT = "EditPlus">
- < METANAME = "Author" CONTENT = "">
- < METANAME = "Keywords" CONTENT = "">
- < METANAME = "Description" CONTENT = "">
- < MetaHttp-equiv = "Content-Type" content = "text/html; charset = gbk"/>
- </HEAD>
- < BODY>
- < FORMMETHOD = "POST" ACTION = "tu. php? Act = upload "enctype =" multipart/form-data">
- < P> < INPUTTYPE = "file" NAME = "file"/> </P>
- < P> < INPUTTYPE = "submit" value = "hao"/> < INPUTTYPE = "reset"/> </P>
- </FORM>
- </BODY>
- </HTML>
PHP code for generating thumbnails
- <? Php
- /* Background login interface
- * Learning and writing
- * Start time:
- * Updated on: 2009/11/24
- */
- Require_once '../xmphp/init. php ';
- // Full-site configuration file
- Require_once XMPHP_COMMON. '/smarty. php ';
- // Smarty function
- Require_once XMPHP_COMMON. '/mysql. php ';
- // Mysql functions
- Require_once XMPHP_COMMON. '/func. php ';
- // Common functions
- If (isset ($ _ GET ['AC']) & $ _ GET ['AC']
= 'Upload '){
- $ File = $ _ FILES ['file'];
- $ Info = pathinfo ($ file ['name']);
- $ A = array ('jpg ', 'GIF', 'PNG ');
- If (! In_array ($ info ['extension'], $ )){
- Alert ("Select Images in jpg, gif, and png formats.
Upload ");
- Exit;
- }
- If ($ file ['name']! = ''){
- $ Dirname = date ('ym ');
- $ Dirname = '../upload/'. $ dirname;
- // Original image path
- $ Xin = $ dirname. '/s'; // PHP generates a thumbnail path.
- If (! File_exists ($ xin )){
- @ Mkdir ($ xin );
- }
- $ Image1 = $ info ['filename']. '000000_000000 '.'.'
. $ Info ['extension']; // Thumbnail name
- $ Image2 = $ info ['filename']. '000000_000000 '.'.'
. $ Info ['extension']; // Thumbnail name
- List ($ imagw, $ imagh) = getimagesize ($ file
['Tmp _ name']); // Obtain the width and height of the source image (in addition, $ file
['Tmp _ name'] is only a temporary file path, and sometimes
An error is reported. If an error is reported, you only need to change the temporary file path to your Upload path.
The path of the source image)
- // Figure 120 determines various situations to obtain the width and height of the thumbnail
- If ($ imagw <= 120 & $ imagh <= 120 ){
- $ M120w = $ imagw;
- $ M120h = $ imagh;
- } Elseif ($ imagw> $ imagh)
- {
- $ M120w = 120;
- $ M120h = intval ($ m120w/number_format (
$ Imagw/$ imagh, 2); // divide the width of the thumbnail by (original
Width divided by height of the source image) number_format query manual
- }
- Else
- {
- $ M120h = 300;
- $ M120w = intval ($ m120h/number_format (
$ Imagh/$ imagw, 2); // divide the height of the thumbnail by (original image
Height divided by the width of the source image) number_format query manual
- }
- // Figure 300 determines various situations to obtain the width and height of the thumbnail
- If ($ imagw <= 300 & $ imagh <= 300 ){
- $ M300w = $ imagw;
- $ M300h = $ imagh;
- } Elseif ($ imagw> $ imagh ){
- $ M300w = 300;
- $ M300h = intval ($ m300w/number_format (
$ Imagw/$ imagh, 2); // divide the width of the thumbnail by (original
Width divided by height of the source image) number_format query manual
- } Else {
- $ M300h = 300;
- $ M300w = intval ($ m300h/number_format (
$ Imagh/$ imagw, 2); // divide the height of the thumbnail by (original
Height of an image divided by the width of the source image) number_format query manual
- }
- If ($ info ['extension'] = 'jpg '){
- $ Info ['extension'] = 'jpeg '; // determines the Image class.
If the image is JPG, convert it to JPEG.
When processing JPG, the processing function uses JPEG instead of JPG.
- }
- $ Iml = 'imagecreatefrom '. $ info ['extension'];
- // Obtain different functions of different image formats
- $ Yuan = $ iml ($ file ['tmp _ name']);
- // Identify the source image based on the different functions obtained earlier
- $ Mu1 = imagecreatetruecolor ($ m120w, $ m120h );
- // ID of the thumbnail to be generated
- $ Mu2 = imagecreatetruecolor ($ m300w, $ m300h );
- // ID of the thumbnail to be generated
- $ C = 'image'. $ info ['extension'];
- // The function for generating the output thumbnail, which cannot be written separately; otherwise, an error is returned.
- Imagecopyresampled ($ mu1, $ yuan, 0, 0,
0, $ m120w, $ m120h, $ imagw, $ imagh); // scale 20
- $ Res = $ c ($ mu1, $ xin. '/'. $ image1 );
- // Store thumbnails
- If (! $ Res ){
- Alert ('generating 120 thumbnails failed ');
- Exit;
- }
- Imagecopyresampled ($ mu2, $ yuan, 0, 0
, 0, 0, $ m300w, $ m300h, $ imagw, $ imagh );
- // Thumbnails generated by PHP are reduced by 20
- $ Res = $ c ($ mu2, $ xin. '/'. $ image2 );
- // Store thumbnails
- If ($ res ){
- Alert ('Success in generating 300 thumbnails ');
- Exit;
- }
- }
- }
- $ Tpl-> display ("tu/tu.html ");
- ?>
The above code example is related to the implementation of generating thumbnails in PHP.
PHP generates the thumbnail HTML code! DOCTYPEHTMLPUBLIC "-// W3C // DTDHTML4.0Transitional // EN"> HTML> HEAD> TITLE> upload image/TITLE> meta name = "Generator" CONTENT =...