Home Backend Development PHP Tutorial How to get remote image url and generate thumbnail in php

How to get remote image url and generate thumbnail in php

Jul 25, 2016 am 08:52 AM

  1. /**

  2. *
  3. *Function: Adjust image size or generate thumbnail
  4. *Return: True/False
  5. *Parameters:
  6. * $Image The image that needs to be adjusted (including path)
  7. * $Dw=450 Maximum width when adjusting; when thumbnailing The absolute width
  8. * $Dh=450 The maximum height when adjusting; the absolute height when thumbnailing
  9. * $Type=1 1, adjust the size; 2, generate thumbnails
  10. */ bbs.it-home.org
  11. $phtypes=array('img/gif', 'img/jpg', 'img /jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');

  12. function compressImg($Image,$Dw,$Dh,$Type) {

  13. IF(!file_exists($Image)){
  14. return false;
  15. }
  16. // If you need to generate a thumbnail, copy the original image and reassign it to $Image (generate thumbnail operation)
  17. // When Type= =1, the original image file will not be copied, but the reduced image will be regenerated on the original image file (resize operation)
  18. IF($Type!=1){
  19. copy($Image,str_replace(" .","_x.",$Image));
  20. $Image=str_replace(".","_x.",$Image);
  21. }
  22. // Get the file type and create different objects according to different types
  23. $ImgInfo=getimagesize($Image);
  24. Switch($ImgInfo[2]){
  25. case 1:
  26. $Img =@imagecreatefromgif($Image);
  27. break;
  28. case 2:
  29. $Img =@imagecreatefromjpeg($ Image);
  30. Break;
  31. case 3:
  32. $Img =@imagecreatefrompng($Image);
  33. break;
  34. }
  35. // If the object is not created successfully, it means it is a non-image file
  36. IF(Empty($Img)){
  37. // If there is an error when generating thumbnails, you need to delete the copied files
  38. IF($Type!=1){
  39. unlink($Image);
  40. }
  41. return false;
  42. }
  43. // If yes To perform the resize operation,
  44. IF($Type==1){
  45. $w=ImagesX($Img);
  46. $h=ImagesY($Img);
  47. $width = $w;
  48. $height = $h;
  49. IF($width>$Dw){
  50. $Par=$Dw/$width;
  51. $width=$Dw;
  52. $height=$height*$Par;
  53. IF($height>$Dh){
  54. $Par= $Dh/$height;
  55. $height=$Dh;
  56. $width=$width*$Par;
  57. }
  58. } ElseIF($height>$Dh) {
  59. $Par=$Dh/$height;
  60. $height= $Dh;
  61. $width=$width*$Par;
  62. IF($width>$Dw){
  63. $Par=$Dw/$width;
  64. $width=$Dw;
  65. $height=$height*$Par;
  66. }
  67. } Else {
  68. $width=$width;
  69. $height=$height;
  70. }
  71. $nImg =ImageCreateTrueColor($width,$height);// Create a new true color canvas
  72. ImageCopyReSampled($nImg,$Img ,0,0,0,0,$width,$height,$w,$h);//Resample copy part of the image and resize it
  73. ImageJpeg($nImg,$Image);//Output the image in JPEG format To the browser or file
  74. return true;
  75. } Else {// If the thumbnail generation operation is performed,
  76. $w=ImagesX($Img);
  77. $h=ImagesY($Img);
  78. $width = $w;
  79. $height = $h;
  80. $nImg =ImageCreateTrueColor($Dw,$Dh);
  81. IF($h/$w>$Dh/$Dw){// Height is larger
  82. $width=$Dw;
  83. $ height=$h*$Dw/$w;
  84. $IntNH=$height-$Dh;
  85. ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w , $h);
  86. } Else {// Larger width ratio
  87. $height=$Dh;
  88. $width=$w*$Dh/$h;
  89. $IntNW=$width-$Dw;
  90. ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
  91. }
  92. ImageJpeg($nImg,$Image);
  93. return true;
  94. }
  95. };
  96. /**
  97. *Get the image on the server based on url
  98. *$url image path on the server $filename file name
  99. */
  100. function GrabImage($url,$filename="") {
  101. if($url=="") return false;
  102. if($filename=="") {
  103. $ext =strrchr($url,".");
  104. if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
  105. return false;
  106. $filename=date ("YmdHis").$ext;
  107. }
  108. ob_start();
  109. readfile($url);
  110. $img = ob_get_contents();
  111. ob_end_clean();
  112. $size = strlen($img);
  113. $fp2 =@fopen($filename, "a");
  114. fwrite($fp2,$img);
  115. fclose($fp2);
  116. return $filename;
  117. }
  118. ?>

Copy Code

Call method:

  1. //Network image path
  2. $imgPath = 'http://news.xxxx.cn/images/1382088444437.jpg';//Remote URL address
  3. $tempPath = 'aa/ bbs.jpg';//Save image path
  4. if(is_file($tempPath)){
  5. unlink($tempPath);
  6. }
  7. $bigImg=GrabImage($imgPath, $tempPath);
  8. compressImg($bigImg, 70,70,1);
  9. ?>
Copy code


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles