php – 图像处理宽度和高度设置

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 图像处理宽度和高度设置脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的项目中我只是做图像水印或图像结合它的工作正常代码.
<!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,$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设置为源图像宽度的一半?

通过亚历克斯你的回答它就像这样.

这会将水印调整为原始图像的半角,并将其放在中心位置:
// 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,$new_width,$new_height,$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,$new_height);

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 图像处理宽度和高度设置全部内容,希望文章能够帮你解决php – 图像处理宽度和高度设置所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。