Crear marcas de agua con PHP

0
768

Añadir o crear marcas de agua a una imagen se utiliza bastante para dejar los créditos en una imagen, realizar esto de forma automática con PHP nos ahorrara tiempo en abrir un programa de edición de imágenes para ponerlo manualmente, ahora solo tendremos que ponerlo como una función antes de hacer el upload o mejor aún si tenemos los archivos en un directorio podemos recorrerlo y agregarle marcas de agua masivamente.
ImageWorkshop es una Clase PHP que utiliza la librería GD para agregar capas de imágenes fácilmente (igual que en Photoshop o GIMP).

Tutorial

Vamos a añadir una marca de agua en una imagen de diferentes formas, solo texto, con imagen, con transparencia, vertical y en diagonal.
Estas son las imágenes que utilizaremos:

1- Agregar texto como marca de agua


[php]
require_once('imageworkshop.php');
$norwayLayer = new ImageWorkshop(array(
"imageFromPath" => "sgs3.jpg",
));
$textLayer = new ImageWorkshop(array(
"text" => "PHP Image Workshop",
"fontPath" => "arial.ttf",
"fontSize" => 14,
"fontColor" => "ffffff",
"textRotation" => 0,
));
$norwayLayer->addLayer(1, $textLayer, 12, 12, "LB");
$image = $norwayLayer->getResult();
header('Content-type: image/jpeg');
imagejpeg($image, null, 95);
[/php]

addLayer([nivel], [subcapa], [x], [y], [posicion]);

En este metodo podemos pasar por

parametros el nivel de la capa (nivel), en que capa se agregara (subcapa), las coordenadas horizontal (x) y vertical (y), y la posicion que podra ser seran LB (Left, Bottom), RT (Right Top), etc.

2- Agregar imagen como marca de agua


[php]
$norwayLayer = new ImageWorkshop(array(
"imageFromPath" => "sgs3.jpg",
));
$watermarkLayer = new ImageWorkshop(array(
"imageFromPath" => "android.jpg",
));
$norwayLayer->addLayer(1, $watermarkLayer, 12, 12, "LB");
$image = $norwayLayer->getResult();
header('Content-type: image/jpeg');
imagejpeg($image, null, 95);
[/php]

3- Agregar imagen con opacidad como marca de agua


[php]
$norwayLayer = new ImageWorkshop(array(
"imageFromPath" => "sgs3.jpg",
));
$watermarkLayer = new ImageWorkshop(array(
"imageFromPath" => "android.jpg",
));
$watermarkLayer->opacity(60);
$norwayLayer->addLayer(1, $watermarkLayer, 12, 12, "LB");
$image = $norwayLayer->getResult();
header('Content-type: image/jpeg');
imagejpeg($image, null, 95);
[/php]

4- Agregar imagen como marca de agua y rotarla


[php]
$norwayLayer = new ImageWorkshop(array(
"imageFromPath" => "sgs3.jpg",
));
$watermarkLayer = new ImageWorkshop(array(
"imageFromPath" => "android.jpg",
));
//$watermarkLayer->opacity(60);
$watermarkLayer->rotate(90);
$norwayLayer->addLayer(1, $watermarkLayer, 12, 12, "LT");
$image = $norwayLayer->getResult();
header('Content-type: image/jpeg');
imagejpeg($image, null, 95);
[/php]

5- Agregar texto en diagonal como marca de agua y rotarla


[php]
function calculAngle($bottomSideWidth, $leftSideWidth)
{
$hypothenuse = sqrt(pow($bottomSideWidth, 2) + pow($leftSideWidth, 2));
$bottomRightAngle = acos($bottomSideWidth / $hypothenuse) + 180 / pi();
return -round(90 – <div style="display: none"><a href=&#039;http://generic–viagra.net/&#039;>buy generic viagra</a></div> $bottomRightAngle);
}
$norwayLayer = new ImageWorkshop(array(
"imageFromPath" => "sgs3.jpg",
));
$textLayer = new ImageWorkshop(array(
"text" => "PHP Image Workshop",
"fontPath" => "arial.ttf",
"fontSize" => 40,
"fontColor" => "ffffff",
"textRotation" => calculAngle($norwayLayer->getWidth(), $norwayLayer->getHeight()),
));
// Some funky opacity
$textLayer->opacity(70);
// We add the $textLayer on the norway layer, in its middle
$norwayLayer->addLayer(1, $textLayer, 0, 0, &#039;MM&#039;);
$image = $norwayLayer->getResult();
header(&#039;Content-type: image/jpeg&#039;);
imagejpeg($image, null, 95);
[/php]
Descargar ejemplos

Documentación

zp8497586rq