I am using PHP GD2 to resize and crop image using below function
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
But I want to create many samples of a single image of different sizes. I am currently repeating same function in single file to get different sizes. But is there any way, perhaps using array or something using which I can achieve the same result?
Related
Hey guys i am trying to crop uploaded image using php using following code.
if(isset($_POST["Crop"])){
$source_x=$_POST['x_value'];
$source_y=$_POST['y_value'];
$width=$_POST['w_value'];
$height=$_POST['h_value'];
$src="assets/images/uploads/1509432972_Koala.jpg";
$crop = imagecreatetruecolor($width,$height);
imagecopy ($crop, $src, 0, 0, $source_x, $source_y, $width, $height);
imagejpeg($crop);
imagedestroy($src);
imagedestroy($crop);
}
The problem that it is not working properly. Since there are lot of posts there to solve this, found a solution called GD library how to include it and crop all kind of images.
I am trying to create the jpeg image using the imagejpeg but that outputs the image to browser and I don't want that.
$im = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($im, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$im=imagejpeg($im, 'test.jpg', 100);
It comes up as if it doesn't have a jpg stored inside $im
I am trying to re-size an image and then work with it afterwards in the jpg format
In PHP, an image hasn't any type once it was created (from imageCreateFromJpeg(), imageCreateTrueColor(), …) : this is simply a ressource.
The conversion in a specific type takes place afterward, when you use imageJpeg() for instance. imageJpeg() outputs the image to browser or saves it in a file. That's all, it returns nothing more than a boolean which say if the conversion was a success or not.
I have been tasked with a problem where i need to pickup one image froma set of 1000 and serve it to the user based on the parameters passed in the get query.
This was simple.
Adiditonally, i have been asked to serve the image in such a way that even if the same file is server everytime, the sha1 hash of the image file should come different.
To achieve this,We could just add random pixels in the image background at random places.
Can somebody tell me how i can acheive this using the GD library
Use Imagesetpixel.
$img = imagecreatefrompng('your_image.png');
$red = imagecolorallocate($img, 255, 0, 0);
imagesetpixel($img, $x, $y, $red);
........
........
On the other hand why would you want to change the sha1 hash of the image on every request?
Edit: Since you want a transparent pixel, you are going to need imagealphablending and something like this:
$img = imagecreatefrompng('your_image.png');
imagealphablending($img, false);
$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagesetpixel($img, $x, $y, $transparent);
imagesavealpha($img, true);
imagepng($img, 'my_saved_file.png');
I am currently trying to get a form which will allow multiple images to be uploaded and resized on the server using PHP. Each uploaded image by the client is around 2.5mb in size.
I am currently using the move_uploaded_file() function.
There are no issues to move the files onto the server. The problem arises when I try to crop. Having not ImageMagick on my host I am using this setup (not all the code just what's relevant, this is in a loop with $width etc. changing for the different crop sizes)
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
As it stands this will only work for 2 images. If there is 3 or more submitted I get a 'memory exhausted' error. I have researched into this as my memory limit is 120mb. Apparently the imagecreatefromjpeg function uses a lot of memory, especially if the file has a large resolution (which mine does - hence why I need them cropped/resized).
Does anyone know of a more efficient approach to this task? I have researched on google but everyone uses the same technique I am.
Use imagedestroy to clear any memory associated with $image and $image_p :
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
imagedestroy($image);
imagedestroy($image_p);
I've got a problem making alpha PNGs with PHP GD. I don't have imageMagik etc.
Though the images load perfectly well in-browser and in GFX programs, I'm getting problems with Flash AS3 (actionscript) understanding the files. It complains of being an unknown type. But, exporting these files from Fireworks to the same spec works fine. So I'm suggesting it's something wrong with the formatting in PHP GD.
There seems to be a number of ways of doing this, with several similar functions; so maybe this isn't right?
$image_p = imagecreatetruecolor($width_orig, $height_orig);
$image = imagecreatefrompng($filename);
imagealphablending($image_p, false);
ImageSaveAlpha($image_p, true);
ImageFill($image_p, 0, 0, IMG_COLOR_TRANSPARENT);
imagealphablending($image_p, true);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width_orig, $height_orig, $width_orig, $height_orig);
imagepng($image_p, "new2/".$filename, 0);
imagedestroy($image_p);
This just takes files it's given and puts them into new files with a specified width/height - for this example it's same as original but in production it resizes, which is why I'm resampling.
To keep the transparency you should do
imagealphablending($image_p, false);
instead of "true". Maybe that will solve the format problem too.