PHP - Getting a small white square when loading in the code - php

<?php
// Set the font file and font size for the text
$font_file = '/path/to/font.ttf';
$font_size = 12;
// Create an array of image filenames
$images = array('image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png');
// Select a random image filename from the array
$random_image = $images[array_rand($images)];
// Load the image using the GD library
$image = imagecreatefrompng($random_image);
// Get the width and height of the image
$width = imagesx($image);
$height = imagesy($image);
// Send the appropriate HTTP headers and output the image
header('Content-Type: image/png');
imagepng($image);
// Free up memory
imagedestroy($image);
?>
*The code works fine, don't get any errors but when I load the code I get this small white square and nothing else. GD is enabled, but don't know how to solve this solution. *

Related

Using Imagick-resizeImage to create thumbnails creates dark images - PHP

I am able to successfully resize (to the proper resized dimensions) an image that I have created from one page of a pdf document. However, I do not understand why the result is a dark resized image with a patch of white. Please, can someone advise?
PHP code:
// Create image from first page of pdf document
$im = new imagick('1Mpublic.pdf[0]');
$im->setImageFormat('jpg');
$imageHeight = $im -> getImageHeight();
$imageWidth = $im -> getImageWidth();
$desiredImgWidth = 200;
$desiredImgHeight = resizedImageHeight($imageWidth, $imageHeight,
$desiredImgWidth);
$im -> resizeImage($desiredImgWidth, $desiredImgHeight,
imagick::STYLE_NORMAL, 1);
// Save image
$page = '1';
$saveToFolder = 'thumbnailFolder';
$fileName = 'thanhThumbNail_'.$page.'.jpg';
$saveImgToPath = $saveToFolder.'/'.$fileName;
$result = file_put_contents($saveImgToPath, $im);
function resizedImageHeight($imgWidth, $imgHeight, $desiredImgWidth){
$quoient = $imgWidth/$imgHeight;
$height = $desiredImgWidth/$quoient;
return $height;
}
Resulting thumbnail image:
Link to original PDF can be found here:
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4905263/pdf/ksgt-06-04-1091539.pdf
Your PDF has transparency. JPG does not support transparency and shows black where the PDF was transparent. Just turn the transparency off. In Imagemagick command line:
convert -density 300 ksgt-06-04-1091539.pdf[0] -alpha off result.jpg
See setImageAlphaChannel at http://us3.php.net/manual/en/imagick.setimagealphachannel.php
Looks like the background color is not defined. You need to set the background color before reading the PDF document.
// Create image from first page of pdf document
$im = new imagick();
$im->setBackgroundColor('WHITE');
$im->readImage('1Mpublic.pdf[0]');
$im->setImageFormat('jpg');

Struggling to change font with GD imagestring

been messing with imagestring trying to change font & font size but i really dont have a clue, i'm new to php and have worked on some code that i came across on a forum and have made it work for me just need to find a way to change font and font size but everything i've tried hasn't given me a good outcome.. any solutions? Please bare in mind i'm new to php and if you could explain as much as possible for your answer thank you so much
$im = imagecreatefrompng("image.png");
// if there's an error, stop processing the page:
if(!$im)
{
die("");
}
// define some colours to use with the image
$black = imagecolorallocate($im, 80, 77, 77);
$font = 'cour.ttf';
// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);
$textt = $_GET['textt'];
$dealer = $_GET['dealer'];
$purchase = $_GET['purchase'];
$name = $_GET['name'];
$address = $_GET['address'];
$address1 = $_GET['address1'];
// store the text we're going to write in $textt
putenv('GDFONTPATH=' . realpath('cour.ttf'));
// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($textt) )/2;
$leftTextPos = ( $width - imagefontwidth($font)*strlen($dealer) )/2;
// finally, write the string:
imagestring($im,$font,$leftTextPos-210,$height-295,$textt,$black);
imagestring($im,$font,$leftTextPos-210,$height-165,$dealer,$black);
imagestring($im,$font,$leftTextPos-200,$height-115,$purchase,$black);
imagestring($im,$font,$leftTextPos-130,$height-70,$name,$black);
imagestring($im,$font,$leftTextPos-100,$height-50,$address,$black);
imagestring($im,$font,$leftTextPos-00,$height-50,$address1,$black);
// output the image
// tell the browser what we're sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($im);
// tidy up
imagedestroy($im);
?>
You should try to use imagettftext function.

Photo color loss after watermarking

Having a few teething problems watermarking a photo. It all works fine apart from the watermarked photo's colors become duller than they should be - very noticeable in-fact.
I'm using imagecopyresized to do my watermarking, as this specifically allows me to use PNG-24 watermarks, the others do not. I know the colors are usually OK, as I have just used readfile($url) as a test, and the photos are perfect.
Here is my script:
<?php
// get parent and watermark images & sizes
$image = imagecreatefromjpeg($url);
$imageSize = getimagesize($url);
$watermark = imagecreatefrompng('watermark.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
// calculate new watermark width and position
if ($imageSize[0] > $imageSize[1] || $imageSize[0] == $imageSize[1]) {
$leftPercent = 23;
} else {
$leftPercent = 7;
}
$leftPixels = ($imageSize[0]/100)*$leftPercent;
$newWatermarkWidth = $imageSize[0]-$leftPixels;
$newWatermarkHeight = $watermark_o_height * ($newWatermarkWidth / $watermark_o_width);
// place watermark on parent image, centered and scaled
imagecopyresized(
$image,
$watermark,
$imageSize[0]/2 - $newWatermarkWidth/2,
$imageSize[1]/2 - $newWatermarkHeight/2,
0,
0,
$newWatermarkWidth,
$newWatermarkHeight,
imagesx($watermark),
imagesy($watermark)
);
// print
imagejpeg($image);
// destroy
imagedestroy($image);
imagedestroy($watermark);
?>
How can I stop this from happening? I'm reading about imagecreatetruecolor, does that solve the issue? I'm Googling "imagecreatetruecolor color loss photos" and variations but nobody really talks about this issue. If I do need this function, where would I add that to this script?
This has totally thrown a spanner in the works for me and would love for somebody to tell me where to stick it (not literally).
Here is an example of the color loss. The preview image should be exactly the same colors as the thumbnail. The thumbnails are created using readfile() whereas the previews are created using imagecreatefromjpeg and imagecopresized.
This example code works fine, by using the same characteristics as your images:
Original JPG: dark background; beautiful girl; red dress.
Watermark PNG: transparent background; text; gray color.
<?php
// Path the the requested file (clean up the value if needed)
$path = $url;
// Load image
$image = imagecreatefromjpeg($path);
$w = imagesx($image);
$h = imagesy($image);
// Load watermark
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
// Merge watermark upon the original image (center center)
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
// Output the image to the browser
header('Content-type: image/jpeg');
imagejpeg($image);
// destroy both images
imagedestroy($image);
imagedestroy($watermark);
// kill script
exit();
?>
Left: Output Image | Right: Original Image
Note:
The output image was compressed several times until: Original -> PHP Output -> GIMP -> Here.
After much testing, I came to the conclusion that PHP's GD Image does not support color profiles on the images that are being watermarked. I am now using Imagick and the colors are perfect.

Passing an Variable from one PHP File to another

I'm working on an image resizer, to create thumbnails for my page. The resizer works on principle of include a DIRECT link to the image. But what I want to do is put in the PHP Variable in the URL string, so that it points to that file and resizes it accordingly.
My code is as follows :
<img src="thumbnail.php?image=<?php echo $row_select_property['image_url']; ?>
Image Resize :
<?php
// Resize Image To A Thumbnail
// The file you are resizing
$image = '$_GET[image_url]';
//This will set our output to 45% of the original size
$size = 0.45;
// This sets it to a .jpg, but you can change this to png or gif
header('Content-type: image/jpeg');
// Setting the resize parameters
list($width, $height) = getimagesize($image);
$modwidth = $width * $size;
$modheight = $height * $size;
// Creating the Canvas
$tn= imagecreatetruecolor($modwidth, $modheight);
$source = imagecreatefromjpeg($image);
// Resizing our image to fit the canvas
imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
// Outputs a jpg image, you could change this to gif or png if needed
imagejpeg($tn);
?>
What I am trying to do is pass on the variable "image=" to the Thumbnail script. At the moment I am passing it through the URL string, but it doesnt seem to load the graphic.
I'll try expand on this more, should you have questions as I am finding it a little difficult to explain.
Thanks in advance.
I suspect at least part of the problem is that your existing...
$image = '$_GET[image_url]';
...line is creating a text string, rather than getting the contents of the 'image_url' query string. Additionally, your passing in the image name as "?image=" in the query string, so you should simply use "image", not "image_url".
As such, changing this to...
$image = $_GET['image'];
...should at least move things along.
Change it
$image = '$_GET[image_url]';
to
$image = $_GET['image'];
$image = '$_GET[image_url]';
should be
$image = $_GET['image'];

resizing with php imagick

I am uploading images and resizing them to 300x200 pixels
i'm using the imagick extension and specifically the adaptiveResizeImage() function.
when uploaded using bestfit there is a lot of whitespace on the sides of the image (depending on portrait or landscape).
what i'd rather it did was resize to fill the entire area (no whitespace) and crop the longer length (height or width), ie i'd rather lose some image than have whitespace.
is there an easy way to do this with the imagick extension?
EDIT: I managed to do what i needed, but there has to be a better way
header('Content-type: image/jpeg');
// target sizes
$target_width = 300 ;
$target_height = 100 ;
// create new image
$image = new Imagick('test.jpg');
// get current size and calculate diffences from target sizes
$size = $image->getImageGeometry();
$size['width_diff'] = $target_width/$size['width'] ;
$size['height_diff'] = $target_height/$size['height'] ;
// resize by smallest size
if($size['width_diff']>=$size['height_diff'])
{
$width = $target_width ;
$height = $size['height']*$size['width_diff'] ;
}
else
{
$width = $size['width']*$size['height_diff'] ;
$height = $target_height ;
}
// get offsets
$x = ($width-$target_width)/2 ;
$y = ($height-$target_height)/2 ;
// resize and offset image
$image->adaptiveResizeImage($width, $height) ;
$image->extentImage($target_width, $target_height,-$x,-$y);
// output and clean up
echo $image ;
$image->clear();
$image->destroy();
Maybe you can take a look at the extent flag in the ImageMagick tutorial.
From a quick look it seems the equivalent PHP would be Imagick::ExtentImage
but have not used it from php.

Categories