Reduce image dimensions php - php

I'm looking to reduce image sizes using PHP on page load. I'm not really to sure how to achieve this, i've gotten as far as to obtain the dimensions but how would I reduce the sizes of these using only PHP?Here is my current code:
<?php
$stmt = $db->query('SELECT * FROM img_gallery ORDER BY list');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$image = $row['url'];
list($width, $height) = getimagesize($image); //grab the image dimensions here
?>
<img src="" width="<?=$width ?>px" height="<?=$height ?>px" /> //image here
So the dimensions are reduced by a few hundred pixels each?

try php imagejpeg
imagejpeg (resource $image [,string $filename [,int $quality ] ] );
For more info click here,

You need to create a new canvas with the desired size and copy the resampled canvas there:
$newcanvas = imagecreatetruecolor($width,$height);
$oldcanvas = imagecreatefromjpeg($imagePath);
imagecopyresampled($newcanvas,$oldcanvas,0,0,0,0,$width, $height,$originalwidth,$originalheight);

Related

Rotate, Crop image using Guillotine plugin and Laravel 5 Intervention

I work on image editor module, using guillotine plugin.
I get parameters from ajax.
{angle: 0,h: 600,scale: 6.7811,w: 800,x: 0,y: 485}
In laravel I have this code
$img = \Input::get('img');
$data = \Input::get('data');
$image = \Image::make($img)->rotate((int)$data['angle']);
// crop image
$image->crop((int)$data['w'], (int)$data['h'], (int)$data['x'], (int)$data['y']);
$image->save('uploads/tmp/img.png');
The code is working , but the result is not the same as the user selected area.
I guess I need to use 'scale' attribute too, but I don't know how.
For example : user selected area
Result
I appreciate your help! :)
you need to use the scale factor
then multiply it by the image width by using the widen() function, it:
Resizes the current image to new width, constraining aspect ratio
so the height will also get scale
$img = \Input::get('img');
$data = \Input::get('data');
list($type, $img) = explode(';', $img);
list(, $img) = explode(',', $img);
$img = base64_decode($img);
// save image
$img = file_put_contents('uploads/tmp/img.png', $img);
$image = \Image::make('uploads/tmp/img.png')->rotate((float)$data['angle']);
//we get the image width then multiply it by the scale factor, it will also scale the height automatically
$image->widen((int)$image->width()*$data['scale']);
// crop image
$image->crop((int)$data['w'], (int)$data['h'], (int)$data['x'], (int)$data['y']);
$image->save('uploads/tmp/img.png');

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" />

How to set original image height & width on resize script?

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

Output wideimage to page

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" />

display image thumbnail by reading path from table and retrieving the image from folder

I would be grateful if anybody could help me out with this.
I have a table which stores the filepath of a set of images, e.g col filepath stores values like: ./phpimages/image3.jpg. Note that my images are stored in folder 'phpimages'
Now i want to loop through all the rows in my table and display thumbnails of the images.
Here is my code:
/
*************************Display all records from images table***************/
//create an instance is Image
$objImage = new Image;
$result = $objImage -> listImage();
$num_rows = mysql_num_rows($result);
//echo $num_rows."records in database";
while($row = mysql_fetch_assoc($result)){
$imagepath="'".$row["filepath"]."'"; // note that if i put $imagepath= "dog.jpg", it displays the image just once!
//set mime type content
header('Content-Type: image/jpeg');
//create original image
$image = imagecreatefromjpeg($imagepath);
//get image dimension
$dim=getimagesize($imagepath);
//Set thumb dimension
$thumbw = 100;
$thumbh = 130;
//create empty image
$thumb_image=imagecreatetruecolor($thumbw, $thumbh);
//Resize original image and copy it to thumb image
imagecopyresampled($thumb_image, $image, 0, 0, 0, 0,
$thumbw, $thumbh, $dim[0], $dim[1]);
//display thumb image
imagejpeg($thumb_image);
}
?>
Please can anyone tell me where my error lies? Many thanks for any help provided
Why not just use <img src="">
You can output only one imagejpeg($thumb_image); using this method. If you want to display all thumbnails in a combined image, you should merge your images to one PHP/GD image, and then output that one.
If you would like to output thumbnail images, then I advise you to use the following tool:
http://phpthumb.sourceforge.net/
So when you iterate through your images, you should output an <img src="" /> for each thumbnail.

Categories