fit image to aspect ratio without croping it - php

I have an image with an aspect ratio of 9x16 (337px, 600px). I want to change the image 16x9 (1066px, 600px) permanently without cropping images using PHP
this is the example that i want before and after.
Im Sorry i dont code yet because i don't know what i suppose to type to type on Google
$imagemal = "https://myanimelist.cdn-dena.com/images/anime/1933/97061l.jpg";
list($width, $height, $type, $attr) = getimagesize("$imagemal");
$newwidth = $height/9;
$newwidth = $newwidth*16;
$newwidth = round($newwidth);

Related

Orientation of photos in CSS form

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

How to resize an image like facebook cover

What basically I am trying to do is to create a cover page for my personal website, just like facebook. Basically I am after the same layout of the cover as on facebook, so that user can get the same result while using the same cover on my site as well as on facebook.
The part I am stucked at is the "Drag image to position cover" thing.
The Facebook uses some algorithm to convert the cover image size to something different during dragging thing. For example, if the original image dimensions are 920x720, the dimensions of same image while it is on facebook setting-cover page(drag image to position cover thing), the dimensions of the image are 851x638.
I just wanted to know what algorithm facebook uses to set the image dimensions(from 720 to 638)
NOTE: The cover has to be the pixel perfect
I know that the cover dimension of facebook is 851x315, so here is what I am doing:
//$x = X origin cordinate variable obtained by dragging image
//$y = Y origin cordinate variable obtained by dragging image
list($k, $l) = getimagesize($src); // $src == image source
//$w = Needs to be calculated
//$h = Needs to be calculated
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( 854,316 );
imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$w,$h,$k,$l);
imagejpeg($dst_r,$src,$jpeg_quality);
$img_name = writeToImage($src, $des); //writeToImage() is just a demo function created by me to do some other things with them which do not affect this part of code
echo $img_name;
I need to figure out how facebook calculates the new dimension of the image from previous one. Is it dependent of the actual(original) size of the image or is it dependent on some other factors?
The formulas for scaling images are quite simple.
$aspect_ratio = $original_width / $original_height;
$new_width = $new_height * $aspect_ratio;
or
$new_height = $new_width / $aspect_ratio;

Maintain aspect ratio of image while uploading

I am uploading image with PHP.
During upload i am resizing image from actual image to needed size.
But image size in not getting in its aspect ration according to width.
PHP Code.
$size = getimagesize($_FILES["file"]["tmp_name"]);
$ratio = $size[0]/$size[1];
$req_width = 500;
$height = $req_width* $ratio*2;
//after this code for re-size image with above dimension and upload image code.
What i am doing wrong in formula.
Why are you multiplying it by 2? And where does $width come from?
You should just be able to divide the required width by the ratio to give you the correct result.
$height = $req_width / $ratio;

getimagesize fails on trying to find out if image is vertical or horizontal

I build a simple function with getimagesize to find out if an image is horizontal or vertical.
// get height and width and then return
list($width, $height) = getimagesize('/path/to/imag.jpg');
if ($width > $height || $width == $height) {
return 'HORIZONTAL';
} elseif ($height > $width) {
return 'VERTICAL';
}
But this seems to fail on some images. Especially on a picture that is taken with a mobile device, holding it vertical. Photoshop opens it and shows it vertical, Mac Preview does too, but getimagesize keeps telling me width is bigger than height.
So I assume the information, this "horizontal" image should be shown "vertical" in Photo application is stored somewhere else.....
But where.... Exif?

How to crop and image before adding an overlay? (GD) (PHP)

My code so far (it creates an overlay to a youtube thumbnail):
<?php
header("Content-type:image/png");
$background=imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert=imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert,0,0));
$insert_x=imagesx($insert);
$insert_y=imagesy($insert);
imagecopymerge($background,$insert,5,57,0,0,$insert_x,$insert_y,100);
imagepng($background,NULL);
The output image is 120x90px, but i need it to have 90x90px.
Anyone knows how this is possible?
Thanks in advance!
http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
<?php
header("Content-type:image/png");
$background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert = imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert,0,0));
list($current_width, $current_height) = getimagesize($background);
$left = 0;
$top = 0;
$crop_width = 90;
$crop_height = 90;
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = $background;
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagecopymerge($canvas,$insert,5,57,0,0,$current_width,$current_height,100);
imagepng($canvas);
?>
try that it should work if not comment as to otherwise.
This is taken from a function I wrote to create square thumbnails. You may find the commentary I wrote for myself helpful. Depending on your needs (i.e if you can't afford to make assumptions about the type and dimensions of incoming images) you may need to add additional checks. To avoid smashed or stretched thumbnails we take a central part of the image which is most likely to contain something distinguishable as source co-ordinates.
The basic idea is: Since imagecopyresampled or imagecopyresized also allow you to specify destination and source coordinates, you can copy only part of your original image to a new canvas and save that (or directly output to browser). To avoid smashed or stretched dimensions we take a central part of the original image which is most likely to contain distinguishable content.
//assuming you have already merged your $background image with your overlay at this point
//original dimensions
$original_width = imagesx($background);
$original_height = imagesy($background);
//new dimensions
$new_width = 90;
$new_height = 90;
//the center of the rectangular image
$center = $original_width/2, $original_height/2
//the coordinates from where to start on the original image
//assuming you want to crop the center part
$src_x = floor(($original_width/2) - ($new_width/2));
$src_y = floor(($original_height/2) - ($new_height/2));
//create a new canvas with the new desired width, height
$temp = imagecreatetruecolor(90,90);
//copy the large image to this new canvas
imagecopyresampled($temp,$background,0,0,$src_x,$src_y,$new_width,$new_height,$original_width,$original_height);
//from right to left: source image, destination image, dest x, dest y,
//source x, source y, new width, new height, original width, original height
//save the canvas as an image
imagepng($temp);
This could be improved to handle larger images by first taking a central part relative to it's size and then scaling it down to the new canvas.

Categories