Imagestring align to the right - php

I want to align the watermark of an image to the right side.
This is what I have so far but its aligned to the left...
// Add Watermark featuring Website Name
$home_url = home_url();
$search = array('http://','https://');
$site_name = str_ireplace($search, '', $home_url);
$watermark = imagecreatetruecolor($width, $height+15);
// Determine color of watermark's background
if (is_array($Meme_Generator_Data) &&
array_key_exists('watermark_background',$Meme_Generator_Data) &&
strlen($Meme_Generator_Data['watermark_background']) == 7)
{
$wm_bg = $this->convert_color(substr($Meme_Generator_Data['watermark_background'], 1));
$bg_color = imagecolorallocate($watermark, $wm_bg[0], $wm_bg[1], $wm_bg[2]);
imagefill($watermark, 0, 0, $bg_color);
}
// Determine color of watermark's text
if (is_array($Meme_Generator_Data) &&
array_key_exists('watermark_text',$Meme_Generator_Data) &&
strlen($Meme_Generator_Data['watermark_text']) == 7)
{
$wm_text = $this->convert_color(substr($Meme_Generator_Data['watermark_text'], 1));
$text_color = imagecolorallocate($watermark, $wm_text[0], $wm_text[1], $wm_text[2]);
} else {
$text_color = imagecolorallocate($watermark, 255, 255, 255);
}
imagestring($watermark, 5, 5, $height, $site_name, $text_color);
imagecopy($watermark, $img, 0, 0, 0, 0, $width, $height);
$img = $watermark;

Replace
imagecopy($watermark, $img, 0, 0, 0, 0, $width, $height);
with
imagecopy($watermark, $img, imagesx($img) - $width, 0, 0, 0, $width, $height);

Related

php imagerotate and imagecopymerge makes no transparent background and no auto width/height

I have this script, which makes an image and posts it on another image:
<?php
$img=imagecreatetruecolor(150,20);
imagealphablending($img,false);
$col=imagecolorallocatealpha($img,255,255,255,127);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
imagefilledrectangle($img,0,0,180,20,$col);
imagealphablending($img,true);
$font='../ttf/0001.ttf';
$color = imagecolorallocate($img, 0, 0, 0);
imagettftext($img,11,0,5,14,$color,$font,'Text goes here');
imagealphablending($img,false);
imagesavealpha($img,true);
imagejpeg($img, '../custom_images/test.jpg');
// Create image instances
$dest = imagecreatefromjpeg('../custom_images/121536.jpg');
$src = imagecreatefromjpeg('../custom_images/test.jpg');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 0, $color);
imagesavealpha($rotated, true);
$trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
imagefill($rotated, 0, 0, $trans_colour);
imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png');
$width = imagesx($new_img);
$height = imagesy($new_img);
// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);
imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
2 things:
Background is not transparent
I want the Width and Height to be automatic, so if the text is short, the image is it to.
What do I fault?

PHP watermark cannot upload png

my script it work with jpg , jpeg , bmp when i upload and succsess.
but when i upload png , gif it stored in my folder but the image is blank with black background color.
is there any way to solve this?
$water_mark_text_2 = "text";
$font_path = "fonts/BRLNSR.TTF";
$font_size = 25; // in pixcels
//$water_mark_text_1 = "9";
function watermark_text($oldimage_name, $new_image_name){
global $font_path, $font_size, $water_mark_text_1, $water_mark_text_2;
list($width,$height) = getimagesize($oldimage_name);
$image = imagecreate(100, 100);
// sets some colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $width, $height);
//imagecopyresampled($image, $stamp, $image_src, 0, 0, 0, 0, $width, $height, $width, $height , imagesx($stamp), imagesy($stamp), 60);
// $black = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 79, 166, 185);
// imagettftext($image, $font_size, 0, 30, 190, $black, $font_path, $water_mark_text_1);
imagettftext($image, $font_size, 0, 5, 20, $white, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
return true;
}
$path = "$stored/";
$valid_formats = array("jpg", "bmp","jpeg","JPG","png");
$name = $_FILES['img']['name'];
list($txt, $ext) = explode(".", $name);
$upload_status = move_uploaded_file($_FILES['img']['tmp_name'],$path.$name);
$new_name = $stored.$md5_img.".jpg";
watermark_text($path.$_FILES['img']['name'], $new_name);
if i change imagecreatefromjpeg to imagecreatefrompng , and imagejpg to imagepng.
the image still upload , but i cannot opened.

black background is coming for transparent images while creating image in php [duplicate]

This question already has an answer here:
PHP & GD - transparent background being filled with nearby color
(1 answer)
Closed 8 years ago.
I am writing the code for watermark in php using below code but for transaparent images, black background is coming:
$font_path = $_SERVER['DOCUMENT_ROOT'] . "/fonts/arial.ttf"; // Font file
$water_mark_text_2 = "IndustrialStores.com"; // Watermark Text
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $owidth;
$height = $oheight;
$image = imagecreatetruecolor($width, $height);
$extension = pathinfo($oldimage_name, PATHINFO_EXTENSION);
$extension = strtolower($extension);
if($extension=="jpg" || $extension=="jpeg" ){
$image_src = imagecreatefromjpeg($oldimage_name);
}
else if($extension=="png"){
$image_src = imagecreatefrompng($oldimage_name);
}
else if($extension=="gif"){
$image_src = imagecreatefromgif($oldimage_name);
}
else if($extension=="bmp"){
$image_src = imagecreatefrombmp($oldimage_name);
}
else{
copy($oldimage_name, $new_image_name);
unlink($oldimage_name);
return true;
}
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate ($image, 179, 179, 179);
$bbox = imageftbbox($width/15, 0, $font_path, 'IndustrialStores.com');
$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2);
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
imagettftext($image, $width/15, 0, $x, $y, $blue, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
I already tried so many other answers fro stackoverflow like using :
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
but there is no use of all this
Would adding these lines help?
else{
copy($oldimage_name, $new_image_name);
unlink($oldimage_name);
return true;
}
// add these lines here!
imagealphablending($image, false);
imagesavealpha($image, true);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate ($image, 179, 179, 179);

PHP GD lib text rendering with pixilation

Here's the problem the text overlayed over the image and gradient has some weird pixilation around the text, but I can't work out why.
-->
Here's the code:
<?php
function hex2rgb($hex) {
$rgb[0] = hexdec(substr($hex, 0, 2));
$rgb[1] = hexdec(substr($hex, 2, 2));
$rgb[2] = hexdec(substr($hex, 4, 2));
return $rgb;
}
$sourceImage = imagecreatefromstring(file_get_contents("source.jpg"));
$imagePadding = 10;
$height = imagesy($sourceImage);
$width = imagesx($sourceImage);
$baseImage = imagecreatetruecolor($width, $height);
$backgroundColor = imagecolorallocate($baseImage, 0, 0, 0);
imagefill($baseImage, 0, 0, $backgroundColor);
imagecopyresampled($baseImage, $sourceImage, 0, 0, 0, 0, $width, $height, $width, $height);
//==== GRADIENT ====//
// Modified from: http://stackoverflow.com/questions/14684622/blend-transparent-gradient-with-an-image-using-php-gd-library
$gradientCanvas = imagecreatetruecolor($width, $height);
$transColor = imagecolorallocatealpha($gradientCanvas, 0, 0, 0, 127);
imagefill($gradientCanvas, 0, 0, $transColor);
$color = hex2rgb("000000");
$start = -40;
$stop = 120;
$range = $stop - $start;
for ($y = 0; $y < $height; ++$y) {
$alpha = $y <= $start ? 0 : round(min(($y - $start) / $range, 1) * 127);
$newColor = imagecolorallocatealpha($gradientCanvas, $color[0], $color[1], $color[2], $alpha);
imageline($gradientCanvas, 0, $y, $width, $y, $newColor);
}
imagecopyresampled($baseImage, $gradientCanvas, 0, 0, 0, 0, $width, $height, $width, $height);
//==== TEXT ====//
putenv('GDFONTPATH=.');
$font = "HelveticaNeueBold.ttf";
$white = imagecolorallocate($baseImage, 255, 255, 255);
imagettftext($baseImage, 14, 0, $imagePadding, $imagePadding + 16, $white, $font, "Foobar");
header('Content-Type: image/jpeg');
imagejpeg($baseImage);
imagedestroy($baseImage);
imagedestroy($sourceImage);
?>
The default quality of imagejpeg http://php.net/manual/en/function.imagejpeg.php is ~75, you will want to set this to 100
imagejpeg($baseImage, null, 100);
The main reason - you are using JPG as output and input image. Editing JPG images often causes the image distortions. Try to use a PNG image as a source.
Or try set the "quality" parameter of imagejpeg() to 100

How to make circled thumbnails using GD

I would like to make a circled thumbnails using GD library. Is there're any ready solutions for this? I've seen libraries only for rounded corners yet.
Here is the script i use. It just overlays my thumbnail with a half-transparent pattern, which makes the thumbnail with some shape effect.
ini_set("memory_limit","64M");
ini_set("gd.jpeg_ignore_warning", 1);
$img_name=$_GET[f];
$type=$_GET[type];
if (isset($img_name)) {
$img_name = $_SERVER["DOCUMENT_ROOT"]."/catalog/".$img_name;
$info = #getimagesize($img_name);
$ext = #$info[2];
$header = #$info['mime'];
$board = ImageCreateFrompng("images/item".$type.".png");
$im = ImageCreateTrueColor(170,140);
$bg = imagecolorallocate($im, 255, 255, 255);
imagefill($im,0,0,$bg);
header("Content-type: $header");
switch($ext) {
case 1: { // GIF
$img = #imagecreatefromgif($img_name);
if ($img) {
imagecopyresized($im, $img, (170-$info[0]), 0, 0, 0, #$info[0], #$info[1], #$info[0], #$info[1]);
imagecopy($im, $board, 0, 0, 0, 0, 170, 140);
imagegif($im, '', 100);
return $chache_fn;
} break;
}
case 2: { // JPG
$img = #imagecreatefromjpeg($img_name);
if ($img) {
imagecopyresized($im, $img, (170-$info[0]), 0, 0, 0, #$info[0], #$info[1], #$info[0], #$info[1]);
imagecopy($im, $board, 0, 0, 0, 0, 170, 140);
imagejpeg($im, '', 100);
} break;
}
case 3: { // PNG
$img = #imagecreatefrompng($img_name);
if ($img) {
imagecopyresized($im, $img, (170-$info[0]), 0, 0, 0, #$info[0], #$info[1], #$info[0], #$info[1]);
imagecopy($im, $board, 0, 0, 0, 0, 170, 140);
imagepng($im, '', 9);
} break;
}
case 6: { // BMP
$img = #imagecreatefromwbmp($img_name);
if ($img) {
imagecopyresized($im, $img, (170-$info[0]), 0, 0, 0, #$info[0], #$info[1], #$info[0], #$info[1]);
imagecopy($im, $board, 0, 0, 0, 0, 170, 140);
imagewbmp($im, '', 100);
} break;
}
}
}

Categories