Is it possible to get the image size in pixels from a canvas?
E.g. I know I can achieve it getting the size from the image directly:
list($width, $height) = #getimagesize($_FILES['inputFieldName']['tmp_name'])
but I want to get it from a canvas. E.g.:
$canvas = imagecreatefromjpeg($image_path);
//Get image size from $canvas
Try:
$canvas = imagecreatefromjpeg($image_path);
$width = imagesx($canvas);
$height = imagesy($canvas);
Details at http://es1.php.net/manual/en/function.imagesx.php and http://es1.php.net/manual/en/function.imagesy.php
Related
Source image width is 344 pixels and height is 86 pixels.
I create new blank image (destination) width 64 pixels and height also 64 pixels.
Then i want to resize source image, so that width is 64 pixels and height is proportionally less.
I did:
Get source image size.
$size = getimagesize( $_FILES['file_to_upload']['tmp_name'] );
Then set new width and height. Initial width is 344 pixels, i need 64, so new width is initial width divided with proportion (344 / 64). New height also is initial height divided with proportion.
$size[0] = $size[0]/($size[0]/64);
$size[1] = $size[1]/($size[0]/64);
Expecting that initial image resizes so that width will be 64 pixels and height 16 pixels.
Create initial image
$src = imagecreatefromstring(file_get_contents( $_FILES['file_to_upload']['tmp_name'] ));
Create destination image
$dst = imagecreatetruecolor($width_64,$height_64);
Create necessary image
imagecopyresampled($dst,$src,0,0,0,0,$width_64,$height_64,$size[0],$size[1]);
imagepng($dst, $img_directory. '/pngicon64_64.png' );
But as result i get this
But i nee to get this
As understand code does not resample initial image. Just take part of it.
May be instead of imagecopyresampled need to use something else?
You have to calculate the image ratio, check which dimension is greater & reduce that one to your desired size, then calculate the smaller dimension using the image ratio.
An extremely similar example is available in the PHP documenation for the imagecopyresampled function. I adapted that example to the code you provided:
// The file
$filename = $_FILES['file_to_upload']['tmp_name'];
// Set a maximum height and width
$width = 64;
$height = 64;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatetruecolor($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagepng($image_p, $img_directory. '/pngicon64_64.png');
I want to know the image width and height of an image from the return image using imageCreateFromBMP($src) rather than $src. How can i achieve this ?
$src = 'four.bmp'; //src of image
$im = imagecreatefrombmp($src);
list($width, $height) = GetImageSize($im); // it doesnot work though it takes string
echo 'Image width '.$width.'</br>';
echo 'Image height '.$height.'</br>';
imagesx() and imagesy() take an image resource
http://php.net/manual/en/function.imagesx.php
http://php.net/manual/en/function.imagesy.php
Example:
$src = 'four.bmp'; //src of image
$im = imagecreatefrombmp($src);
$width = imagesx($im);
$height = imagesy($im);
echo 'Image width '.$width.'</br>';
echo 'Image height '.$height.'</br>';
I have an image file that is stored within the variable $image I want to resize this image so it would fit within an area of 380px by 380px (so which means that the tallest side of the image must be 380px on the other side smaller than 380px).
Has anyone a suggestion on how to do this?
Thanks
here is what I use to keep it under 800x600
$orig_image = imagecreatefromjpeg($file['tmp_name']);
list($width,$height) = getimagesize($file['tmp_name']);
if(max($width,$height) > 800){
$scale = 800/max($width,$height);
$new_width = floor($width*$scale);
$new_height = floor($height*$scale);
$save_image = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($save_image,$orig_image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($save_image,self::$FILE_DIRECTORY."$year_month/$fileId.jpg");
$orig_image = $save_image;
$width = $new_width;
$height = $new_height;
}
hopefully you can extrapolate a solution off that.. also not that my $file variable is coming from an uploaded file in the $_FILE array.
I am sure this topic has been discussed number of times and I have started doing this from yesterday evening but till now no complete satisfaction.I am using the following code and it gives me a resized image but the scaling is not correct.The height and width is not in it's correct proportion.
I couldn't find any good fiddle like jsfiddle where you can see the output so pasting my code here.
You can see the original image url here also I have attached a resized image.
http://distilleryimage4.s3.amazonaws.com/b1da08e4484511e38e4d0a7011810191_7.jpg
$filename = 'http://distilleryimage4.s3.amazonaws.com/b1da08e4484511e38e4d0a7011810191_7.jpg';
//the resize will be a percent of the original size
$percent = 0.589;
$percent_height = 0.294;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent_height;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
imagedestroy($thumb);
This is basically a Math problem:
You are scaling width and height to both different proportions ($percent and $percent_height). You should scale both of them (width and height) by the same percent, to get an image resized to the same ratio. Maybe I didn't understand your question but I think you should change this line:
$newheight = $height * $percent_height;
to
$newheight = $height * $percent;
(and remove $percent_height = 0.294; if we are not gonna use it here)
I would use Imagick. I was doing resizing with imagecreatetruecolor, but it takes a lot of time (0,5 seconds) to resize 1920*1080 image to 150*120.
If you still want to use imagecreatetruecolor: to get the correct scalling use this code.
How can I get the width and height of the image after rotating it using imagerotate() in PHP?
Here is my code:
<?php
// File and rotation
$filename = 'test.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);
?>
But what I want to do before doing the output, is that I want to get the width and height of the rotated image. How can I do that?
I'm sure you can do something similar to this:
$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];
Another option is this:
list($width, $height) = getimagesize($filename);
imagerotate returns an image ressource. Hence you cannot use getimagesize which works with an image file.
Use
$width = imagesx($rotate);
$height = imagesy($rotate);
instead.