Trouble changing image size in PHP? - php

I have the following PHP code...
$destination_image_x = "235";
$destination_image_y = "230";
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);
$source_image_x = imagesx($temp_profile_picture_converted);
$source_image_y = imagesy($temp_profile_picture_converted);
$temp_profile_picture_converted = imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($temp_profile_picture_converted, $user_profile_picture_filename,'75');
imagedestroy($temp_profile_picture_converted);
The function of this code is to scale an image passed to it, and save it at a specified directory. I'm able to save the image using "imagejpeg" normally if I ommit the resizing snippet. The variable "$temp_profile_picture_converted" is assigned to a jpg image I created from the user's uploaded image with "imagecreatefromjpeg." (Or imagecreatefrompng, or imagecreatefromgif, etc.)

You are using the same variable $temp_profile_picture_converted twice in the following line. The function imagecopyresampled() returns a boolean and is overwriting the image this variable holds. The return value from this function is only to check success. Change it to:
if (! imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y)){
// then give error message...
}
UPDATE
However, you have other errors. You need to change the first parameter of imagejpeg(). I also changed the size vars from strings to numbers - not sure if it mattered.
imagejpeg($destination_image, $user_profile_picture_filename,75);
I successfully ran the following code
$destination_image_x = 235;
$destination_image_y = 230;
$source_image_x = imagesx($temp_profile_picture_converted);
$source_image_y = imagesy($temp_profile_picture_converted);
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);
imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($destination_image, $user_profile_picture_filename,75);
imagedestroy($temp_profile_picture_converted);
imagedestroy($destination_image);
Note that I also added the last statement imagedestroy($destination_image);

Related

Retrive PHP function confusion [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 months ago.
The project i'm working right now require to use some color randomization. I have atleast 20 asset (text,shape,line,polygon ...etc) that require color randomization ...so i decided to store the randomization RGB in a function so i could keep reusing it and make the PHP file smaller but it didn't work out as expected
<?php
header ('Content-Type: image/jpeg'); // make PHP when access .jpg
$im = imagecreatetruecolor(100, 100); // allocate image with 100 width and 100 height
function randomrgb() {
imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); // generate 0 between 255
}
$cbc = randomrgb(); // store randomization on variable first i do not know how to call functions directly
imagefill($im, 0, 0, $cbc); // fill the background with $cbc which is equal to random RGB function
imagejpeg($im);
imagedestroy($im);
?>
I expected the background color to become random but it always black also i do not know how to call the function directly here imagefill($im, 0, 0, $cbc); without storing it first on variable something like imagefill( $im, 0, 0, randomrgb(); ); i know it possible but i just do not know how .. sorry in advance if this is confusing
In PHP (and many other programming languages) , any variable used inside a function is by default limited to the local function scope .
For your case, please
pass the $im (after you used imagecreatetruecolor to create the image identifier) as parameter to the function; and
add return in your function, so as to assign the result to $cbc
Hence, change to:
<?php
header ('Content-Type: image/jpeg'); // make PHP when access .jpg
$im = imagecreatetruecolor(100, 100); // allocate image with 100 width and 100 height
function randomrgb($im) {
return imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); // generate 0 between 255
}
$cbc = randomrgb($im); // store randomization on variable first i do not know how to call functions directly
imagefill($im, 0, 0, $cbc); // fill the background with $cbc which is equal to random RGB function
imagejpeg($im);
imagedestroy($im);
?>

Converting JPEG/PNGs to PNG for database storage but can't keep background transparency/colour when displaying, in PHP

I've a script I use to convert JPEGs or PNGs to PNGs for database storage, I encode the file into base64 and store it in a database, I pull the file from the database or cache and when displaying it on a website I just pull the image data from the database or cache and do a base64 decode on it and display it using a standard IMG tag.
The issue I'm having is, no matter if the background is transparent or what colour I create as the image's background it always shows up as black when being displayed on the webpage.
I've tried numerous of the questions on the right side and none of the answers seems to work for me.
Encode Function
public function encode($image, $resize = false, $dirLevel = '')
{
$vTempFileName = TEMP_DIR . '_'.rand(1111111, 9999999) . '.png';
// Convert image to PNG
$image = imagecreatefromstring(file_get_contents($image));
imagealphablending($image, true);
imagepng($image, $vTempFileName);
$vImageDetails = array(base64_encode(file_get_contents($vTempFileName)), filesize($vTempFileName));
// Remove temporary file after processing
#unlink($vTempFileName);
return $vImageDetails;
}
Decode Function
echo base64_decode($image);
I've also tried using the below two functions
To save transparency you need to add imagesavealpha See http://php.net/manual/en/function.imagesavealpha.php
public function encode($image, $resize = false, $dirLevel = '')
{
$vTempFileName = TEMP_DIR . '_'.rand(1111111, 9999999) . '.png';
// Convert image to PNG
$image = imagecreatefromstring(file_get_contents($image));
imagealphablending($image, true);
imagesavealpha($image, true);
imagepng($image, $vTempFileName);
$vImageDetails = array(base64_encode(file_get_contents($vTempFileName)), filesize($vTempFileName));
// Remove temporary file after processing
#unlink($vTempFileName);
return $vImageDetails;
}
imagefill will perform a flood fill on the selected coordinates with the desired color defined as $transparent See: http://php.net/manual/en/function.imagefill.php
$fillColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $fillColor);
To flood fill the adjoining color at the top left of the image with the desired fillColor.
Otherwise to add transparency use imagecolortransparent see http://php.net/manual/en/function.imagecolortransparent.php
$replaceBlack = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $replaceBlack);
to replace the color black as transparent.
This may or may not help you but... you really don't want to store images into a database if you can avoid it. Is there a good reason you are doing this? File systems are good at storing binary files. Databases are good at storing data. Use the appropriate tool for the job – store it as a file.

PHP set the attribute for an image with matrix values

as the topic,I need to set the attribute for an image or create an image with the attribute what I get from a client as the matrix values.
I found a function named imageconvolution,but it doesn't work out.Maybe I used it incorrectly.
here is the code:
<?php
$image = imagecreatefromgif('http://www.php.net/images/php.gif');
$emboss = array(array(0, 0, 100), array(0, 0, 200), array(0, 0, 1));
imageconvolution($image, $emboss, 1, 0);
header('Content-Type: image/png');
imagepng($image, null, null);
?>
the matrix values are used to scale or rotate or move the image.Is these code right?
I hope to find out someone to teach me.
Thanks a lot.
Scaling, rotation and moving are affine transforms. You cannot use convolution for this matrixes.
I think the easiest way to use the php Imagick extension. It has an affineTransformImage function which you can use: http://php.net/manual/en/imagick.affinetransformimage.php

Resize with imagecopyresampled and imagejpeg not working

I'm trying to resize a picture using the following PHP script.
$tn = imagecreatetruecolor(1836, 3264);
$newImage = imagecreatefromjpeg('user/354010050076877/2.jpg');
imagecopyresampled($tn, $newImage, 0, 0, 0, 0, 1836, 3264, 739, 1162);
imagejpeg($tn, 'MyFile.jpg');
The image is created at MyFile.jpg but it's still the original size.
I also tried replacing line 4 with ...
file_put_contents('MyFile.jpg', $tn);
When I try that it returns
"Warning: file_put_contents(): supplied resource is not a valid stream resource in /home/content/01/7258201/html/imgTools/resize.php on line 6"
What do I need to change in my script to get image resize working?
EDIT:
I had mixed the order of values on 'imagecopyresampled' however even after switching them It's not really resizing correctly so I'm still looking for a good fix for this. See my own answer for more details.
Try this:
$tn = imagecreatetruecolor(739, 1162); // the first line in your script
I tested your script (that one using imagejpeg) and it works on my end. So probably something in your GD library configuration/setting ...
Problem was I had put the wrong values for new width and height and mixed them with the old changed.
imagecopyresampled($tn, $newImage, 0, 0, 0, 0, 1836, 3264, 739, 1162);
to
imagecopyresampled($tn, $newImage, 0, 0, 0, 0, 739, 1162, 1836, 3264);
Thought it's still not completely working as it resizes the old image onto a larger black space.

Tiling images with PHP GD

I'm trying to tile multiple images, i.e. put one directly underneath another. They all have the same width (120px) and differing heights.
This is what I have:
$finalbg = null;
for($i=0; $i<7; $i++) {
$addbg = imagecreatefromjpeg('images/left/'.$url[$drawn]);
$addsize = imagesy($addbg);
if($finalbg != null) $basesize = imagesy($finalbg); else $basesize = 0;
$newsize = $addsize+$basesize;
$newbg = imagecreatetruecolor(120, $newsize);
if($finalbg != null) imagecopy($newbg, $finalbg, 0, 0, 0, 0, 120, $basesize);
imagecopy($newbg, $addbg, 0, $basesize, 0, 0, 120, $addsize);
$finalbg = $newbg;
}
header( "Content-type: image/jpeg" );
imagejpeg($finalbg);
The sizes are outputting correctly, but it keeps telling the image contains errors, and I have no idea why :( Same thing if I try to output addbg or newbg.
Thanks.
Okay, apparently the problem was that there was HTML on the page that was supposed to be rendered, which turns out not to be possible in combination with a GD image.
So I took a different approach. I saved the rendered image as a file, like so:
imagejpeg($finalbg, 'images/left/bg.jpg');
and set it as the background in CSS. And now it works!

Categories