图片处理宽高设置

image manipulation width and height setting

在我的项目中,我只是做图像水印或图像组合,它工作正常并为此编写代码。

<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image    
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];

$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname= time();

$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);

// Set the thumbnail name
$thumbnail = $folder.$newname.".".$ext; 
$imgname=$newname.".".$ext;

// Load the mian image
if ($ext=="png" || $ext=="PNG") {
$source = imagecreatefrompng($uploadimage);
}
else if ($ext=="gif" || $ext=="GIF") {
$source = imagecreatefromgif($uploadimage);
}
else if ($ext=="bmp" || $ext=="BMP") {
$source = imagecreatefrombmp($uploadimage);
}
else{
$source = imagecreatefromjpeg($uploadimage);
}

// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');

// get the width and height of the watermark image
$water_width = imagesx($source)/2;
$water_height = imagesy($watermark);

// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);

$im_middle_w = $main_width/2;
$im_middle_h = $main_height/2;

// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis 
$dime_x = $im_middle_w - $water_width/2;
$dime_y = $im_middle_h - $water_height/2;

// copy both the images
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);

// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
unlink($file);
}
?>
<img src='uploads/<?php echo $imgname;?>'>
</body>
</html>

但是设置 $water_width 时出现问题,我想将其设置为源图像的一半。但是当我的源图像宽度小于或大于 $water_width 时,它就是这样设置的。当源图像宽度更大时查看图像。

当宽度较小时。 所以我的问题是如何将 $water_width 设置为源图像宽度的一半?

Alex 你的回答是这样的。

这会将水印的大小调整为原始图像的一半宽度并将其置于中心:

// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');

// get the width and height of the watermark image
$water_width = imagesx($watermark);
$water_height = imagesy($watermark);

// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);

// resize watermark to half-width of the image
$new_height = round($water_height * $main_width / $water_width / 2);
$new_width = round($main_width / 2);
$new_watermark = imagecreatetruecolor($new_width, $new_height);
// keep transparent background
imagealphablending( $new_watermark, false );
imagesavealpha( $new_watermark, true );

imagecopyresampled($new_watermark, $watermark, 0, 0, 0, 0, $new_width, $new_height, $water_width, $water_height);

// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis 
$dime_x = round(($main_width - $new_width)/2);
$dime_y = round(($main_height - $new_height)/2);

// copy both the images
imagecopy($source, $new_watermark, $dime_x, $dime_y, 0, 0, $new_width, $new_height);

// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);

imagedestroy($source);
imagedestroy($watermark);
imagedestroy($new_watermark);

你可以试试imagettftext方法,如果你不想要这么完美的透明度。你可以试试这个代码。你必须在你的目录中保存一个字体文件,这里我使用 arial.ttf.

$im = imagecreatefrompng("png.png");  //create image data
$font = 'arial.ttf';  //font file name
$randomString  = "example.com";   //string need to be shown
$main_width = imagesx($im);       //finding width and height
$main_height = imagesy($im);      
$posx= $main_width/2;    //finding center 
$posy = $main_height/2;
$color = imagecolorallocate($im, 200, 200, 200);  //Creating color 
$size = ($main_width/25)+1;     //determine size of font. +1 to avoid 0
$temp = $size*5;     
$posx = $posx-$temp;    //adjust to average center
imagettftext($im,$size,0, $posx, $posy, $color, $font , $randomString);   //apply a text

您必须针对文本位置调整 posxposySize也可以通过一些逻辑来调整

$color = imagecolorallocate($im, 0, 0, 0);=黑色

$color = imagecolorallocate($im, 255, 255, 255); = 白色。

您必须根据需要的文本颜色调整此项。