I am looking for a solution to upload an image, check the resolution crop it if necessary then then preview the image before it then gets printed onto a canvas.
I have found loads of examples of this but obviously nothing straight forward, plus i don't want to rip off someone elses work.
ASP is NOT an option as the site uses PHP and is on a 'nix box.
Any pointers would be much appreciated.
Basically the file will need to be POSTed to your PHP script. You can do this with HTML forms(input type='file') or jQuery background uploaders. Uploads are stored by default in the temporary directory of your web server, the current directory can be found by looking at the output of phpinfo().
Next check out the $_FILES global specifically move_uploaded_file(), you can find the image type from the type key in the $_FILES array, though that can be manipulated client side it it might be better to check the file type manually using magic byte functions.
Once you have the file uploaded then you need to manipulate it. You can use either GD or ImageMagick, ImageMagick may not be built-in to your PHP version, GD is pretty common. I am more familar with GD, so I'll suggest you check out the functions imagecreatefromjpeg()/png/gif & imagecopyresampled(), which can both crop and resample. To find out if you need to crop / resize you can check the aspect ratio. Here's a function I whipped up:
function fixRatio($x, $y, $ratio) {
$ratio = round($ratio,6);
$iRatio = round($x / $y, 6);
if ($iRatio > $ratio) {
$x = ceil($y * $ratio);
} else if ($iRatio < $ratio) {
$y = ceil($x * (1 / $ratio));
}
return array('x' => $x, 'y' => $y);
}
You input the width, height & desired aspect ratio & this will spit out an array that contains the corrected dimensions. It's probably best to specify the ratio by dividing desired height by desired width, e.g. fixRatio($x, $y, 640/480). You can then use that info to crop the image w/ imagecopyresampled(), at the same time you can specify to the function the destination width & height. So if everything must be 640x480, then you would specify that when calling imagecopyresampled(). You can then use iamgejpeg()/gif/png to output the image as a file to a public directory on your server. Since it's public you can reference it via a URL be it an HTML form asking the user to confirm if they like what they see or into a canvas element.
Related
Let's suppose I want to create a face generator, where I would design several pictures for face form, ears, noses, mouth, hair. Given a combination of those pictures how can I create a new picture in PHP? For instance, a simplified version:
There is face1.png, face2.png, nose1.png and nose2.png. How could I programmatically merge face1.png with nose2.png, so the result picture would hold content from both picture?
You've basically got three options: GD, Cairo or ImageMagic. I'd recommend using the Imagick class if it's available. If not, ImageMagick through PHP system calls. If that's not available, GD will probably suffice.
It depends on your server configuration, which of these are available and which would require additional packages to be installed.
There's a very simple example in the Imagick documentation of combining images: https://secure.php.net/manual/en/imagick.compositeimage.php
I also found this example for GD:
Merge two PNG images with PHP GD library
There is a function named imagecopy. This function overrides a part of the destination image using a source image. The given part is specified as parameters. Before you tell me that this does not solve your problem, I have to add that the pixels in the destination picture will not be overriden by the pixels of the source picture if the pixels are transparent. You need to use imagealphablending and imagesavealpha on the source picture, like this:
public static function experimental($images, $width, $height, $dest = null) {
$index = 0;
if ($dest === null) {
$dest = $images[$index++];
}
while ($index < count($images)) {
imagealphablending($images[$index], true);
imagesavealpha($images[$index], true );
imagecopy($dest, $images[$index++], 0, 0, 0, 0, $width, $height);
}
return $dest;
}
If we have these two pictures:
The result will be this:
You really want make it with PHP?
Ok.
1) You can use GD library for image processing.
2) You can use imagemagic PHP wrapper -- imagic.
But I think you should use canvas on client side. And for saving result you can send base64 png image representation of canvas (or separated layers/pictures) to backend.
I’m creating a web application that uses the SoundCloud API to stream tracks of artists. I know how I can get the waveform PNG image (http://w1.sndcdn.com/fxguEjG4ax6B_m.png for example), but I actually need some sort of wave-data (when in the song is it high and when is it low?).
I don’t have access to an audio library like LAME or something like that, because my web hosting doesn’t allow it. Is it possible to
Get this data directly from the SoundCloud API in some way.
Process the waveform PNG image in PHP or JavaScript to retrieve the needed data? (And is there maybe some sort of library available for this kind of processing?)
Soundcloud starts to provide the floating points but it’s not official yet. Just a little trick, when you have your PNG:
https://w1.sndcdn.com/XwA2iPEIVF8z_m.png
Change "w1" by "wis" and "png" by "json":
https://wis.sndcdn.com/XwA2iPEIVF8z_m.json
And you get it!
It's possible to parse the waveform PNG image to translate it to an array of points. The images are vertically symmetrical and to find the peaks you only need to inspect the alpha values to count how many opaque pixels it is from the top of the image. This is how the waveforms are rendered for the widget and on the Next SoundCloud.
In PHP, you could use ImageMagick or the GD Graphics Library to read these values, and in Javascript, it's possible by putting the image onto a canvas object and then inspecting the image data from there. I won't go too much into the details of these, but you could certainly ask another question if you get stuck.
While there is no official way to get the raw waveform data directly from a SoundCloud API request, there is a way to derive the exact same data SoundCloud reveals in the unofficial endpoint (aka: Something like https://wis.sndcdn.com/XwA2iPEIVF8z_m.json) in PHP using this code like this. Simply change the value of $image_file to match whatever SoundCloud 1800 wide by 280 high PNG image you have and you are good to go:
$source_width = 1800;
$source_height = 140;
$image_file = 'https://w1.sndcdn.com/XwA2iPEIVF8z_m.png';
$image_processed = imagecreatefrompng($image_file);
imagealphablending($image_processed, true);
imagesavealpha($image_processed, true);
$waveform_data = array();
for ($width = 0; $width < $source_width; $width++) {
for ($height = 0; $height < $source_height; $height++) {
$color_index = #imagecolorat($image_processed, $width, $height);
// Determine the colors—and alpha—of the pixels like this.
$rgb_array = imagecolorsforindex($image_processed, $color_index);
// Peak detection is based on matching a transparent PNG value.
$match_color_index = array(0, 0, 0, 127);
$diff_value = array_diff($match_color_index, array_values($rgb_array));
if (empty($diff_value)) {
break;
}
} // $height loop.
// Value is based on the delta between the actual height versus detected height.
$waveform_data[] = $source_height - $height;
} // $width loop.
// Dump the waveform data array to check the values.
echo '<pre>';
print_r($waveform_data);
echo '</pre>';
The benefit of this method is while that https://wis.sndcdn.com/ URL is useful, there is no telling if/when SoundCloud would change the structure of the data coming from it. Deriving the data from the official waveform PNG offers some long term stability since they are not just going to change that PNG image without fair warning to SoundCloud API end users.
Also, note that while the $source_width is 1800 the $source_height is 140 because while the SoundCloud PNG file is 280 pixels high, the bottom half is basically just a flipped/mirrored copy of the top half. So just measuring the values from 0 to 150 will give you the necessary waveform data values.
Sorry to bump an old thread - just in case you are looking for something similar and stumbles across this post: This is now possible as per this link: Waveforms, Let's Talk About Them.
It was published shortly after this thread - so again apologies for bumping an old one.
how to take image from one folder and rename and re-size the images and move to another folder?
I need to get image from one folder and rename and re-size the same image and move into another folder using php
You will most likely be using gd for resizing the images.
Here is a pretty crappy, but hopefully useful code sample. In this case, $originalName is the name given in the $_FILES array's tmp_name position. I am resizing to a width of 1200 in this case, with the height adapting according to such width. You might (and most likely will) not desire this behavior. This is just some ugly code I used in some courses I taught about 3 years ago, I don't have the newer samples in this computer so you will have to get used to the idea :)
$newDir is where the file will be located. by calling imagejpeg or imagepng and passing the filename as second argument, it means to the function that you wish to save the image in that location.
if ($type == 'image/jpeg') {
$original = imagecreatefromjpeg($originalName);
}
else {
$original = imagecreatefrompng($originalName);
}
$width = imagesx($original);
$height = imagesy($original);
//prepare for creation of image with width of 1000
$new_height = floor($height * (1200 / $width));
// create the 1200 width image
$tmp_img = imagecreatetruecolor(1200, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $original, 0, 0, 0, 0,
1200, $new_height, $width, $height);
//create a random and unique name to identify (here it isn't that random ;)
$newDir = '/this/is/some/directory/and/filename.';
if ($type == 'image/jpeg') {
imagejpeg($tmp_img, $newDir."jpg");
}
else {
imagepng($tmp_img, $newDir."png");
}
Many file system functions are already built-in with PHP (e.g. rename), and you'll find most of what you need to resize images by using the GD library here.
There are libraries available in PHP for image resize.
Here are some useful links you may like.
http://www.fliquidstudios.com/2009/05/07/resizing-images-in-php-with-gd-and-imagick/
http://php.net/manual/en/book.image.php
PHP/GD - Cropping and Resizing Images
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
Use imagick http://php.net/manual/en/imagick.resizeimage.php
If I were you I would write a PE using a diffent language (one that you might be best used to) to adjust anything of the given image then just feel free to phpexec it to do all the steps you mentioned, you can sit relax and wait for the end result. HHAHAHHA :-)
Use imagick library to resize; it's good.
does anyone know how to apply fade effect to an image using PHP ? what I am looking for is a way to apply gradient transparency ( i mean : at the top , the image is opaque , which gradually gets more and more transparent , and at the bottom it is completely transparent).
i have been reading up on http://php.net/manual/en/function.imagecolortransparent.php , but did not see anything about applying a gradient effect to an image.
i also read : PHP - Generate transparency (or opacity) gradient using image , but it kinda trailed off without any solution!
I am also open to any other suggestion / libraries that can do this from command line.
Obviously you'll need to work with a png for this effect, but you can convert any png into a jpg using php. The following question I believe covers what you are asking about. Part of the code will have to be removed to clear the image reflection effect.
Can You Get a Transparent Gradient using PHP ImageMagick?
The piece of code which seems to do what you are trying to accomplish is:
$im = new Imagick('image.jpg'); //Reference image location
if (!$im->getImageAlphaChannel()) {
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET);
}
$refl = $im->clone();
$refl->flipImage();
$gradient = new Imagick();
$gradient->newPseudoImage($refl->getImageWidth() + 10, $refl->getImageHeight() + 10, "gradient:transparent-black");
I am looking to add a background to images that users upload that are not square. So if they upload a tall and skinny photo I want to add a white background to the sides of the image to make the resulting image have an aspect ratio of 1:1. Is this possible using PHP or javascript?
You can use the GD library for what, with a library called Wideimage it's a breeze:
$image = WideImage::load('img_form_field_name');
$size = max($image->getHeight(), $image->getWidth());
$white = $image->allocateColor(255, 255, 255);
$image->resizeCanvas($size, $size, 'center', 'center', $white);
See the documentation and examples, many functions can even be tested interactively.
The GD library is the most commonly used image manipulation package. It's a set of functions often installed with PHP which handle image manipulation.
What you'll want to do is either scale and crop your image to a specific aspect ratio so that you place your image on a square canvas and cut off whatever does fit or
You'll want to simply resize your image to a fixed aspect ratio and place it on a square canvas with whitespace around it.
Either way, this tutorial should point you in the right direction
http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/
Yep you'll want to look into either the GD library or ImageMagik. There are plenty of tutorials available for this task.
Functions like imagecreatetruecolor() etc will allow you to create a new image, and then stack the uploaded image on top of it and save it as a new file.
Yes.
http://www.php.net/manual/en/refs.utilspec.image.php