$src = imagecreatefromjpeg( $filename );To read from an image binary data, you need to use function imagecreatefromstring and pass image binary data as argument:
$src = imagecreatefromstring( $image_data );In either case, the returned value $src will be a PHP resource type and can be referenced later. Now you need to tell the script the size of the new image going to be. In this article I will only resize the image based on height, you can change the script accordingly if you want to resize the based on image width.
$size = 60; $width = imagesx( $src ); $height = imagesy( $src ); $aspect_ratio = $height / $width;Function imagesx and imagesy will give us the actual images width and height respectively. Now we can start resize the image.
// start resizing if ( $height <= $size ) { [tab]$new_w = $width; [tab]$new_h = $height; } else { [tab]$new_h = $size; [tab]$new_w = abs( $new_h / $aspect_ratio ); }We are saying here that if image's original height is less than the specified size, then we don't change the image size, otherwise calculate the new width and height. Lastly we use function imagecreatetruecolor to create a new image based on the new width and height.
$img = imagecreatetruecolor ( $new_w, $new_h ); // output image imagecopyresampled( $img, $src, 0, 0, 0, 0, $new_w, $new_h, $width, $height ); imagejpeg( $img );Here we don't want to stretch or shrink the image, so we pass four 0s in the imagecopyresampled parameters. If you want to output the image to a file rather than display directly, pass filename as extra argument.
imagejpeg( $img, $filename );