Output wideimage to page - php

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

Related

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

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

PHP + JS: Determine dimensions of an image file

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

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.

Add styled backgrounds to images in PHP

I am working on improving one of my Facebook apps by allowing the user to upload an image and apply a styled border, or frame to it (i.e. clouds, stars, sky etc). The user chould also be able to save the image, with the border after it has been applied. This explains a little better what I need:
http://zbrowntechnology.info/ImgDisp/imgdisp.php
If you have any other questions or need more details, please let me know.. I'll edit this post.
Use imagecopy(). The example on that page is done using the transparency option with imagecopymerge() but I don't think you need that.
Using imagecopy() you'll specify the X/Y coordinates to use for positioning:
imagecopy( $borderimage, $topimage, 20, 20, 0, 0, $width, $height);
Where $width and $height will be the entire width and height of the top image. You'll want to replace 20 and 20 with the measurement for how much of the border image will be showing around the borders. You will probably have to resize the top image to the exact dimensions you want, or else it might overlap the border a little too far to the right or bottom. (see imagecopyresampled())
Edit:
Here's a rough way to do the whole process (assuming chosenborder.png is the border they chose, and uploadedimage.png is the image they uploaded. If it's a different image type you'll use the corresponding function).
$borderx = 20; // The width of our border
$border = imagecreatefrompng("chosenborder.png");
$topimage = imagecreatefrompng("uploadedimage.png");
$bordersize = getimagesize($border);
$topimagesize = getimagesize($topimage);
/* The new dimensions of topimage. $borderx*2 means we account for
the border on both left and right, top and bottom. */
$newx = $bordersize[0] - ($borderx*2);
$newy = $bordersize[1] - ($borderx*2);
imagecopyresampled( $topimage_scaled, $topimage, 0, 0, 0, 0,
$newx, $newy, $topimagesize[0], $topimagesize[1]);
/* Merge the images */
imagecopy( $border, $topimage_scaled, $borderx, $borderx,
0, 0, $width, $height);
/* Output the image */
imagepng($border, "newimage.png");
/* Free up the memory occupied by the image resources */
imagedestroy($border);
imagedestroy($topimage);
After the user uploads their image, find chosenborder.png and uploadedimage.png, run the above script, then display newimage.png to the user and you're good to go. Just make sure you call imagedestroy() on the temporary image resources or they'll eat up memory.
If you don't want to keep the generated image on your server, you can omit the second argument to imagepng() which will make it send the image information directly as an image to the browser, in which case you'll want to write the correct image HTTP headers.
Client-side solution by using css3:
checkout the css3 property border-image
(dosen't meet the requirement of saving the img with the border)
Server-side solution by merging 2 different images:
<?php
$imgFile = 'img.jpg';
$brdFile = 'brd.jpg';
$img = addBorder($imgFile,$brdFile);
outputImage($img);
function addBorder($imgFile,$brdFile)
{
$img=imagecreatefromjpeg($imgFile);
$brd=imagecreatefromjpeg($brdFile);
$imgSize = getimagesize($imgFile);
$brdSize = getimagesize($brdFile);
//NOTE: the border img MUST be bigger then the src img
$dst_x = ceil(($brdSize[0] - $imgSize[0])/2);
$dst_y = ceil(($brdSize[1] - $imgSize[1])/2);
imagecopymerge ( $brd, $img, $dst_x, $dst_y, 0, 0, $imgSize[0], $imgSize[1] ,100 );
return $brd;
}
function outputImage($img)
{
header('Content-type: image/png');
imagepng($img);
}
?>

Categories