


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)
?>

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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
