search
HomeBackend DevelopmentPHP TutorialPHP uploads pictures and adds watermarks (picture watermarks, text watermarks)_PHP tutorial

PHP uploads pictures and adds watermarks (picture watermarks, text watermarks)_PHP tutorial

Jul 13, 2016 am 10:45 AM
phpuploadaddpicturepictureexisttidyletterpaymentComparewatermarkuserof

php upload pictures and add watermarks (picture watermarks, text watermarks) This is a relatively complete software that automatically adds watermarks to pictures when users upload them. This watermark adding function can add text watermarks and picture watermarks.

php tutorial to upload pictures and add watermarks (picture watermarks, text watermarks)
This is a relatively complete software that automatically adds watermarks to pictures when users upload them. This watermark adding function can add text watermarks and picture watermarks.

/*
* created on 2010-6-21
*
* the class for control image
*
* made by s71ence
*
* @$img_path image path
* @$is_auto_reduce Whether the image is automatically compressed according to the size level 1 is
* @$is_appoint Whether to manually compress or amplify 1 Yes
* @$multiple Manually specify compression/amplification ratio
* @$is_water_str Whether to add watermark text 1 is
* @$water_str watermark text
* @$is_watermark Whether to add watermark to the picture 1 Yes
* @$logo_path Watermark image path
* @$is_display Whether to display pictures 1 is
* @$is_create Whether to generate compressed images 1 is
*
* Note:
* 1. Images cannot be displayed when generating new images, that is, $isdisplay and $iscreate cannot be set to 1 at the same time
* 2. When the image width or height is less than 1000, you need to manually set the compression ratio for compression
* 3. It is not recommended to enable watermarks. If you want to enable it, it is recommended that the original image size should be within 1000
* 4. The watermark text cannot contain Chinese
* 5. The newly generated image is in the original directory file and supports n levels
*/

class image_control
{
private $img_path;
private $is_auto_reduce;
private $is_appoint;
private $multiple;
private $is_water_str;
private $water_str;
private $is_watermark;
private $logo_path;
private $is_display;
private $is_create;

function __construct($img_path,$is_auto_reduce,$is_appoint,$multiple,$is_water_str,$water_str,$is_watermark,$logo_path,$is_display,$is_create)
{
$this->img_path=$img_path;
$this->is_auto_reduce=$is_auto_reduce;
$this->is_appoint=$is_appoint;
$this->multiple=$multiple;
$this->is_water_str=$is_water_str;
$this->water_str=$water_str;
$this->is_watermark=$is_watermark;
$this->logo_path=$logo_path;
$this->is_display=$is_display;
$this->is_create=$is_create;
}

function img_control()
{
//Get the original image
$img_info=getimagesize($this->img_path);

switch($img_info[2])
{
case 1:
$img_get=@imagecreatefromgif($this->img_path);
Break;

case 2:
$img_get=@imagecreatefromjpeg($this->img_path);
Break;

case 3:
$img_get=@imagecreatefrompng($this->img_path);
Break;
}

//Text watermark
if($this->is_water_str==1)
{
//imagettftext(original image, text size, text rotation, watermark starting coordinate x, watermark starting coordinate y, $te,'simhei.ttf',$str);
$te=imagecolorallocate($img_get,255,255,255);
$str=iconv("gbk","utf-8",$this->water_str);//Watermark text
Imagettftext($img_get,16,0,$img_info[0]-200,$img_info[1]-20,$te,'msyh.ttf',$str);
}

//Picture watermark
if($this->is_watermark==1)
{
//Watermark image processing
$logo_info=getimagesize($this->logo_path);

switch($logo_info[2])
{
case 1:
$logo=@imagecreatefromgif($this->logo_path);
Break;

case 2:
$logo=@imagecreatefromjpeg($this->logo_path);
Break;

case 3:
$logo=@imagecreatefrompng($this->logo_path);
Break;
}

//Watermark logo image
//Function description: imagecopy(original image, watermark image, watermark coordinate x, watermark coordinate y, watermark image start coordinate x, watermark image start coordinate y, 'watermark image width', 'watermark image height');
Imagecopy($img_get,$logo,0,0,0,0,$logo_info[0],$logo_info[1]);
}

//Automatic image compression Automatic compression based on image size classification
//imagecopyresized(canvas, original image, canvas starting x coordinate, canvas starting y coordinate, original image starting x coordinate, original image starting x coordinate, new image width, new image height, original image width, original image height );
if($this->is_auto_reduce==1)
{
If($img_info[0]>=3000 || $img_info[1]>=3000)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.03,$img_info[1]*0.03);//Generate canvas
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.03,$img_info[1]*0.03,$img_info[0],$img_info[1]);
}
else if($img_info[0]>=2500 || $img_info[1]>=2500)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.04,$img_info[1]*0.04);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.04,$img_info[1]*0.04,$img_info[0],$img_info[1]);
}
else if($img_info[0]>=2000 || $img_info[1]>=2000)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.05,$img_info[1]*0.05);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.05,$img_info[1]*0.05,$img_info[0],$img_info[1]);
}
else if($img_info[0]>=1500 || $img_info[1]>=1500)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.08,$img_info[1]*0.08);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.08,$img_info[1]*0.08,$img_info[0],$img_info[1]);
}
else if($img_info[0]>=1000 || $img_info[1]>=1000)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.1,$img_info[1]*0.1);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.1,$img_info[1]*0.1,$img_info[0],$img_info[1]);
}
else if($img_info[0]>=500 || $img_info[1]>=500)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.2,$img_info[1]*0.2);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.2,$img_info[1]*0.2,$img_info[0],$img_info[1]);
}
else if($img_info[0]>=300 || $img_info[1]>=300)
{
$new_image_get=imagecreatetruecolor($img_info[0]*0.3,$img_info[1]*0.3);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.3,$img_info[1]*0.3,$img_info[0],$img_info[1]);
}
else
{
$new_image_get=imagecreatetruecolor($img_info[0]*1,$img_info[1]*1);
Imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*1,$img_info[1]*1,$img_info[0],$img_info[1]);
}
}

//Manual image compression
//imagecopyresized(canvas, original image, canvas starting x coordinate, canvas starting y coordinate, original image starting x coordinate, original image starting x coordinate, new image width, new image height, original image width, original image height );
if($this->is_appoint)
{
$new_image_get=imagecreatetruecolor($img_info[0]*$this->multiple,$img_info[1]*$this->multiple);//Generate canvas
imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*$this->multiple,$img_info[1]*$this->multiple,$img_info[0],$img_info [1]);
}

//Image output
if($this->is_display==1)
{
Header("content-type: image/jpeg");
Return imagejpeg($new_image_get);
}

//New image generation
if($this->is_create==1)
{
$new_name=explode("/",$this->img_path);
$new_name_string="";

for($i=0;$i {
$new_name_string.=$new_name[$i]."/";
}

$new_img_path=$new_name_string."new".$new_name[$i];

if(imagejpeg($new_image_get,$new_img_path) && imagejpeg($img_get,$this->img_path))
{
setcookie("img_new_path", $new_img_path);
//return "Image generated successfully!
New image: ".$new_img_path."
Original image: ".$this->img_path;
}
else
{
Return "The image generation failed, please check whether the configuration is correct!";
}
}
}

function __desctruct()
{
//clear
}
}

//Call method

/* $img_path="../users/user_photo/t2.jpg"; //The path of the image being manipulated
$is_auto_reduce=1;//Whether the image is automatically compressed according to the size level 1 is
$is_appoint=0;//Whether to compress manually 1 is
$multiple=0.5;//Manually specify the compression ratio
$is_water_str=0;//Whether to add watermark text
$water_str="www.bKjia.c0m";//Watermark text
$is_watermark=0;//Whether to add watermark to the picture 1 is
$logo_path="../image/logo_about.gif";//Watermark image path
$is_display=0;//Whether to display the picture 1 is
$is_create=1;//Whether to generate compressed images 1 is
$img=new image_control($img_path,$is_auto_reduce,$is_appoint,$multiple,$is_water_str,$water_str,$is_watermark,$logo_path,$is_display,$is_create);
echo $img->img_control();*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633043.htmlTechArticlephp upload pictures and add watermarks (picture watermarks, text watermarks) This is a relatively complete and reasonable method for users to upload pictures Automatically add watermarks to pictures whenever you want. This watermark adding function can add text...
Statement
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

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 Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.