How can you tell the dimensions of a .jpeg or .png file without opening it?
If it is impossible to determine the dimensions, how can I automatically crop an image?
I am using PHP and JS.
list($width, $height) = getimagesize("img.jpg");
or
$im=imagecreatefromjpeg("image_testin_1.JPG");
imagetruecolortopalette($im, false, 255);
$w = imagesx($im);//provide width of full page
$h = imagesy($im);//provide height of full page
Related
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
I've finally been able to complete the script for an multiple image resizer, currently it's resizing the original image into 3 other sizes, but I am unable to figure out how to set the original height and width. I have used the getimagesize() but it does not seem to work.
The whole code is here but I don't think it's necessary to post all of it here. http://pastebin.com/UR75tdj3
I have done the following to set each of the images height and width I'd like them to resize into.
$uploadedfile = $_FILES['file']['tmp_name'];
list($width,$height)= getimagesize($uploadedfile);
#large
$largeWidth = 670;
$largeHeight = 330;
$largeTmp = imagecreatetruecolor($largeWidth, $largeHeight);
#medium
$mediumwidth = 330;
$mediumheight = 330;
$mediumTmp = imagecreatetruecolor($mediumWidth,$mediumHeight);
#small
$smallWidth = 327;
$smallHeight = 158;
$smallTmp = imagecreatetruecolor($smallWidth,$smallHeight);
but I wanted to enter the orignal into another folder as well, so I did the following thinking that getimagesize($_FILES['file']['tmp_name']) would return them correctly but it did not.
#original
$originalWidth = $width; //here and
$originalHeight = $height; // here
$originalTmp = imagecreatetruecolor($originalWidth,$originalHeight);
So how do I get the original image height and width as I have tried to do above?
$originalWidth and $originalHeight should return the specific images width & height, but it does not, that is the only issue I am having.
you want to check the size of
$_FILES['file']['tmp_name']
the actull uploaded file as stored on the system
not $_FILES['file']['name'] which is just the filename
I'm trying to use the WideImage plugin and loading an image into it, resizing it, and then outputting it in the following form:
<img class="image" src="image.jpg" />
I have this so far:
<?php
$image = WideImage::load('image.jpg');
$resizedImage = $image->resize(150, 150, 'outside')->crop('center', 'middle', 150, 150);
?>
How can I output the resized image so that it's in the form above? Help!
Resize
You can resize images by passing a few parameters to the resize() method. The first two are the new dimensions of the image, and can be smart coordinate values. If one dimension isn’t specified (or null is given), it’s calculated from the ratio of the other dimension.
Resize an image into a 400×300 box. By default, resizing keeps the original image’s aspect ratio and the resulting image fits the given dimensions from the inside.
$resized = $image->resize(400, 300);
This is equal to passing ‘inside’ as $fit value.
$resized = $image->resize(400, 300, 'inside');
Resize an image to fit a 400×300 box from the outside by passing ‘outside’ to $fit parameter. This means that the image will be at least as big as 400×300, and aspect ratio will be kept.
$resized = $image->resize(400, 300, 'outside');
Resize an image to exactly fit a 400×300 box by passing ‘fill’ as the value of $fit parameter. The image will be stretched as necessary, aspect ratio may not be kept.
$resized = $image->resize(400, 300, 'fill');
The fourth parameter ($scale) determines when to scale an image. Possible values include any (default), down and up:
down – resize if image is larger than the new dimensions
up – resize if image is smaller than the new dimensions
any – resize regardless of the image size
There are two aliases for the resize method: resizeUp and resizeDown. These two are equal to calling resize() with $scale = ‘up’ and $scale = ‘down’ respectively.
$resized = $image->resize(350, 500, 'inside', 'down');
// is equal to
$resized = $image->resizeDown(350, 500, 'inside');
in your HTML add
<img src= "<?= $resized ?>"> – Moises Zaragoza just now edit
The problem is that the browser attempts to read the image as if it was text, more specifically an html document.
To solve it I would create a new php script for the image processing, let's say image.php, with a function taking the image path and also the parameters of the modification.
In the end just show the image as you did before, just be sure to change the header of the content to something like:
header('Content-type: image/jpg');
or
header('Content-type: image/png');
With that the browser will know how to interpret the data received.
First you have to save the resized image as a file.
In you php put:
<?php
$image = WideImage::load('image.jpg');
$resizedImage = $image->resize(150, 150, 'outside')->crop('center', 'middle', 150, 150);
$resizedImage ->saveToFile('resized_image.jpg');
?>
Then make your image reference the new file:
<img class="image" src="resized_image.jpg" />
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;
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.