Because I personally need to upload pictures to the website album and display them in the form of thumbnails, Baidu searched a lot of source codes and tried and failed. I wrote a function myself and it has run successfully. The way to display thumbnails in the browser is to point the src address of the image to a PHP file and call this function to display it.
/** * _thumb()生成缩略图的函数 */ function _thumb($_filename,$_max_size){ //(图片源地址,最大宽or高) //获取文件后缀 $_n = explode('.', $_filename); //生成png表头文件 header('Content-type:image/png'); //获取文件的长和高 list($_width,$_height) = getimagesize($_filename); //生成微缩的长和高 $_percent = $_max_size / (($_width > $_height) ? $_width:$_height); $_new_width = $_width * $_percent; $_new_height = $_height * $_percent; //创建一个微缩画布 $_new_image = imagecreatetruecolor($_new_width, $_new_height); //按照已有的图片创建一个画布 switch ($_n[1]) { case 'jpg': $_image = imagecreatefromjpeg($_filename); break; case 'png': $_image = imagecreatefrompng($_filename); break; case 'gif': $_image = imagecreatefromgif($_filename); break; } //将原图采集后重新复制到图上,就缩略了 imagecopyresampled($_new_image, $_image, 0,0,0,0, $_new_width, $_new_height, $_width, $_height); imagepng($_new_image); imagedestroy($_new_image); imagedestroy($_image); }
The above introduces how to generate image thumbnails with PHP and display them in the browser. It is flexible and practical, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.