get Width and height of image php from imageCreateFromBMP($src) - php

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>';

Related

Crop image using php and jquery

This code is use to save a cropped image in the same place.
In this code crop image from specific point the datax is x point of canvas
top position where crop start and
datay is y point of canvas top position and
datawidth is width of canvas
dataheight is height of canvas
img_scr path of file
canvas use for crop image
Below is the code described above:
$targ_w = 396; // set the width of the new image
$targ_h =400; //set the height og the new image
$src = $_POST['img_src']; //get the image source
$img_r = imagecreatefromjpeg($src); //open the image
$dst_r = imagecreatetruecolor( $targ_w,$targ_h); //create a new image
imagecopyresampled($dst_r,$img_r,0,0,$_POST['datax'],$_POST['datay'],$targ_w,$targ_h,$_POST['datawidth'],$_POST['dataheight']); // create the new image with the specified width and height from jcrop
unlink($src); //delete the old image
imagejpeg($dst_r,$src,100); // save the new image
imagedestroy($img_r);
Before crop
After Crop image is shrinking
Please try below script for cropping image.
<?php
if (isset($_FILES["file"])) {
$tmpFile = $_FILES["file"]["tmp_name"];
$fileName = 'file name';
list($width, $height) = getimagesize($tmpFile);
// check if the file is really an image
if ($width == null && $height == null) {
header("Location: index.php");
return;
}
// resize if necessary
if ($width >= 400 && $height >= 400) {
$image = new Imagick($tmpFile);
$image->thumbnailImage(400, 400);
$image->writeImage($fileName);
}
else {
move_uploaded_file($tmpFile, $fileName);
}
}
It would be helpful...

Resizing Images PHP

I'm using a url link to resize images, such as:
image.php?name=butterfly&size=1100x1100
for example:
<img src="image.php?name=butterfly&size=1100x1100">
The code I'm using is:
<?php
if(isset($_GET['name'])){ //name
$image['name'] = $_GET['name'];
} else {
$image['name'] = null;
}
if(isset($_GET['size'])){ //dimensions
$image['size'] = $_GET['size'];
$size = explode('x', $image['size']);
$image['width'] = $size[0];
$image['height'] = $size[1];
} else {
$image['size'] = null;
}
if(isset($_GET['text'])){ //text
$image['text'] = $_GET['text'];
} else {
$image['text'] = null;
}
// File and new size
$filename = 'images/'.$image["name"].'.jpeg';
// Content type
header('Content-Type: image/jpeg');
// Output
imagecreatefromjpeg($filename);
// Set a maximum height and width
$width = $image['width'];
$height = $image['height'];
// 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 = imagecreatefromjpeg($filename);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>
My code works only for one part, which is the width, the image is resized to it's width but not height. Also when I resize my window, the picture get smaller every time. Thank you for your time and sorry for any bad explanation.
First of all, what is the question? I didn't get it.
One reason why the code changes only the width it may be because of this part of the code:
`if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}`
only one of the sizes will change. Also, another reasons why is changing only the width could be that you have as original image and quadratic resolution (e.g. 800x600 - ratio 1,34) and then you keep changing it to a wider one (e.g. 1280x720 - ratio 1,77).
Hope this helps!
As per your code logic, at any given time either your height or your width will change.
I tried it at my local host, width & height of my image was 600 X 400 and I passed 100X100 as parameter, it resized the image to 100X66, so the height did changed.

How to get image size from a canvas?

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

How to obtain width and height of image after rotating it?

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.

PHP image dimension

How to get width and height of an image?
I have $variable = 'http://site.com/image.png"
Want to get width of this image to $width and height to $height.
Like:
$variable = 'http://site.com/image.png"
$width = '300'; // image width is 300px
$height = '500'; // height is 500px
Thanks.
$variable = 'http://site.com/image.png';
$image = getimagesize($variable);
$width = $image[0];
$height = $image[1];
$type = $image[2];
getimagesize function. Maybe it will help you.

Categories