


Code sharing for php to generate thumbnails by percentage_PHP tutorial
So I read the manual, understood a few functions, and wrote a simple PHP program to generate thumbnails. Without classes, I think a function can do it, and it is easier for novices to understand, which can help more people.
Supports abbreviation according to score ratio, supports abbreviation according to specified length and width, and defaults to percentage. The comments in the program are very detailed. If you have any questions, please leave a message below. Welcome to communicate with me.
The source code is as follows:
/*
* param ori_img The name and path of the original image
* param new_img The name of the generated image
* param percent means to abbreviate according to the percentage of the original image. When this item is empty, it defaults to 50%
* param width specifies the abbreviated width
* param height specifies the abbreviated height
*
* Note: When percent width height is passed in, and percent>0, priority is given to Abbreviate by percentage
* by: http://www.jb51.net More source code to share with you
* Warm reminder: To use this function, you need to enable gd2 in php.ini
*
**/
function makeThumb($ori_img, $new_img, $percent=50, $width=0, $height=0){
$original = getimagesize($ori_img); / /Get the picture information, you can print_r($original) and find that it is an array
//$original[2] is the picture type, where 1 means gif, 2 means jpg, and 3 means png
switch($original [2]){
case 1 : $s_original = imagecreatefromgif($ori_img);
break;
case 2 : $s_original = imagecreatefromjpeg($ori_img);
break;
case 3 : $s_original = imagecreatefrompng($ori_img);
break;
}
if($percent > 0){
$width = $original[0] * $percent / 100 ;
$width = ($width > 0) ? $width : 1;
$height = $original[1] * $percent / 100;
$height = ($height > 0) ? $height : 1;
}
//Create a true color canvas
$canvas = imagecreatetruecolor($width,$height);
imagecopyresized($canvas, $s_original, 0, 0, 0, 0, $width, $height, $original[0], $original[1]);
//header("Content-type:image/jpeg");
// imagejpeg($canvas); //Output the image to the browser
$loop = imagejpeg($canvas, $new_img); //Generate a new image
if($loop){
echo "OK!
";
}
}
makeThumb("bhsj.jpg","suolue1.jpg",15,0,0); //Generate 15% of the original image Thumbnail
makeThumb("bhsj.jpg", "suolue2.jpg",0,200,120); // Generate a thumbnail with a width of 100px and a height of 60px
makeThumb("bhsj.jpg", "suolue3. jpg",15,200,120); //Generate a thumbnail of 15% of the original image (when all parameters are filled in, the percentage priority is higher)
?>

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software