Orientation of photos in CSS form - php

I am makin form which taking photos from my database "ready to print" on A4 paper.
Some photos are orientated on height eg: 800x600 & some are eg 600x800. I need some php script which automaticly rotate horizontal photo to vertical & vertaly photos keep in their orientation.

you can use imagerotate with php..
http://www.php.net/manual/fr/function.imagerotate.php

you need something like this:
$filename="image.jpg";
// get the width and height from the image
list($width, $height, $type, $attr) = getimagesize($filename);
//if image width is bigger then the height it will execute the following script
if ($width > height){
// Load the image
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
//and save it on your server...
file_put_contents("myNEWimage.jpg",$rotate);
}
You might do some adjustments and testing. are busy at work atm so dont have time to test it.
Greetings

Related

Best approach to create thumbnails?

Im working on a template for a website that has already more than 50,000 articles and images assigned to every article.
Before now the article image was visible only inside every article, but now I would like to use thumbnails.
I don't have access to modify the upload image form, so the solution should be something like virtual thumbs created from the original images...
What will be the best approach in this case?
Using Mr. Thumb like I advised a simple script to get it working would be
<?php
include './mrthumb.class.php';
// The image you are resizing. Can be a local path as well.
$image = $_GET['i'];
$quality = 100; // percent
// In this example we are resizing the image in proportionate sizes.
// Below we are specifying the MAX width and height.
$width = 100; // Pixels
$height = 130; // Pixels
// Start Mr. Thumb v1.0
$mrthumb = new MrThumb();
// Render the image
$mrthumb->render( $image );
// Resize the image proportionately
// $mrthumb->constrain( $width, $height );
$mrthumb->proportion( $width, $height );
// Finally, output the image to the browser!
// Optionally we can save the image to a destination
// $mrthumb->saveto( $destination, $filename, $quality );
$mrthumb->output( $quality );
// Clean up after you are done! ;)
$mrthumb->clear_cache();
?>
Then save that to your web server along with the mrthumb class and call a thumbnail in your webpage like
<img src="./mrthumb.php?i=images/myimage.jpg" alt="My Image" />

Automatically rotate image when is width bigger than height in PHP

I asked in past, but I am not sure we understand & I still havent solution.
I need elegant solution when;
I have photo 600x800 & I need show it on my site rotated on 90 degrees, so result will be, when I print php page all photos will be verticaly automaticly.
e.g.
I have a lot of photos, two kinds: 800x600 & 600x800.
I need on my php page showed all of them 800x600 in original and all 600x800 rotated on 90 degrees.
I need some really simple solution, I am out of mind totally. Some function which can rotate images which have bigger width than height.
Thanks a lot.
Using PHP function getimagesize() you can get width and height of your image:
list($width, $height) = getimagesize($imageUrl);
Then, in your template:
<?php if($width > $height): ?>
//put css here as you want
<?php else: ?>
//put css here as you want
<?php endif; ?>
Use getimagesize for detect image width and if width is 800px add inline css like this
style="-moz-transform: rotate(90deg); -webkit-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);"
getimagesize() function will give you width and height and by using imagerotate() you can rotate that image to any desired angle.
Try this:-
<?php
// File and rotation
$filename = 'php.jpg';
$degrees = 180;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>

Rotate image on top of background-image

I have two images. one is a jpg image of a rotated polaroid frame polaroid.jpg. The other is just an ordinary image image.jpg.
I'm trying to rotate the image, and then put it on top of the polaroid-image, and then show the merged images as one jpg-image.
I think I'm pretty close with the following code, but I can't manage to get the transparancy working. The uncovered zone of the rotated image is black instead of transparent. What am I doing wrong? I've added a comment to the lines that are relevant for getting a transparent background for the top-image.
$bg_src = "polaroid.jpg";
$img_src = "image.jpg";
$outputImage = imagecreatefromjpeg($bg_src);
$img = imagecreatefromjpeg($img_src);
// This should create transparent background.
$bgd_color = imagecolorallocatealpha($img, 0, 0, 0, 127);
// This should assign the transparent background to the uncovered zone after rotation
$img = imagerotate($img, 10, $bgd_color);
// This should make sure the alpha transparency gets saved
imagesavealpha($img, true);
$img_x = imagesx($img);
$img_y = imagesy($img);
imagecopymerge($outputImage,$img,156,50,0,0,$img_x,$img_y,100);
header('Content-type: image/jpeg');
imagejpeg($outputImage);
imagedestroy($outputImage);
Figured it out after some heave searching. Turns out to be real simple.
I just changed this line:
imagesavealpha($img, true);
to this:
imagecolortransparent($img,$bgd_color);
yay! :)

fixed size thumbnails like twitpic.com

How do you create a fixed size (height / width) of images/thumbnails In GD?
I know there is a lot of php scripts out there but that just scale it and height/width will always be different size.
I like the thumbnail like twitpic.com and facebook
You need to get the height and width of the image using getimagesize
and then resize it using imagecopyresized
The rest is all the same basic work done with GD to load and save the image.
Here's a basic example, if you want to take into account height/width ratios, then you have to do some additional maths.
<?php
header("Content-type: image/png");
$size = getimagesize($filename);
$image = imagecreatefrompng($filename);
$thumbnail = imagecreate(100,100);
imagecopyresized($thumbnail, $image, 0, 0, 0, 0, 100, 100, $size[0], $size[1]);
imagepng($thumbnail);
imagedestroy($image);
imagedestroy($thumbnail);
It's easy with Thumbnailer:
$th=new Thumbnailer("your-photo.jpg");
$th->thumbSquare(100)->save("thumb.jpg");

Using GD to change the color of a one color shape on a transparent background while preserving transparency

I have a png that is a set of white shape on a transparent background. I'm trying to change to color of the shapes while preserving the transparent background. I've been experimenting with the code below which does change the color but results in a black background. I think the imagetruecolortopalette is causing the problem but the color doesn't change if I remove that line.Any suggestions?
<?php
$imgname = "whiteim.png";
$im = imagecreatefrompng ($imgname);
imagetruecolortopalette($im,false, 255);
$index = imagecolorclosest ( $im, 255,255,255 ); // get White COlor
imagecolorset($im,$index,255,0,0); // SET NEW COLOR
$imgname = "result.png";
imagepng($im, $imgname ); // save image as png
imagedestroy($im);
?>
# imagecolortransparent($im, $xxxx); //not sure why this works
I think this work because imagecolortransparent makes the given color (where you placed $xxxx) transparent, in this case $xxxx contains no value. So what is made transparent are all the pixels that contain no color value.
One thing is I couldn't make that working using imagetruecolortopalette either. Not quite sure if you can use the imagefill function in your case (you need to know where to start the fill and it works if you have one area of white), but this is what I've used.
The other thing is that it seems like you need to call imagesavealpha before you save any alpha information to a png image, otherwise it's lost. Hard to tell for me why isn't it a default setting.
All in all, my approach was:
$imgname = "whiteim.png";.
$im = imagecreatefrompng ($imgname);
imagefill($im, 0,0, imagecolorallocate($im, 255,0,0));
$imgname = "result.png";
imagesavealpha($im, True);
imagepng($im, $imgname ); // save image as png
imagedestroy($im);

Categories