Jump to content

Help creating JPG


rondog

Recommended Posts

I am currently generating a thumbnail with this script:

<?php
header("Content-type: image/jpeg");
$src 		= $_GET['fp'];
$img 		= imagecreatefromjpeg("../../../../../".$src);
$width 		= imagesx($img);
$height 	= imagesy($img);
$new_width 	= 150;
$new_height	= floor($height * ($new_width / $width));
$tmp_img 	= imagecreatetruecolor($new_width, $new_height);

imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($tmp_img,NULL,40);
imagedestroy($tmp_img);

?>

 

It creates it fine. My question is, while most of them are 16x9 images, their is an occasional 9x16. Is their a way to crop the 9x16 to 16x9 so they are all the same image size?

Link to comment
https://forums.phpfreaks.com/topic/205789-help-creating-jpg/
Share on other sites

How do you mean? Crop an image that is 9px width and 16px high, to 16px wide and 9px high? That doesn't make sense, at least to me. How can you crop something to a larger width?

 

You might want to resize it, but that could distort the image.

 

Sorry I meant aspect ratio 16x9, not pixels. I def should have specified.

 

Andy that looks like what I need thanks.

Link to comment
https://forums.phpfreaks.com/topic/205789-help-creating-jpg/#findComment-1077215
Share on other sites

That link was golden. I found this function and it works perfect:

function croppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) //$imgSrc is a FILE - Returns an image resource.
{ 
    //getting the image dimensions 
    list($width_orig, $height_orig) = getimagesize($imgSrc);  
    $myImage 	= imagecreatefromjpeg($imgSrc);
    $ratio_orig = $width_orig / $height_orig;
   
    if ($thumbnail_width / $thumbnail_height > $ratio_orig)
{
       $new_height 	= $thumbnail_width / $ratio_orig;
       $new_width 	= $thumbnail_width;
    }
else
{
       $new_width 	= $thumbnail_height * $ratio_orig;
       $new_height 	= $thumbnail_height;
    }
   
    $x_mid = $new_width / 2;  //horizontal middle
    $y_mid = $new_height / 2; //vertical middle
   
    $process = imagecreatetruecolor(round($new_width), round($new_height));
   
    imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
    $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
    imagecopyresampled($thumb, $process, 0, 0, ($x_mid - ($thumbnail_width / 2)), ($y_mid - ($thumbnail_height / 2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);

    imagedestroy($process);
    imagedestroy($myImage);
    return $thumb;
}


$newThumb 	= croppedThumbnail('AnImage.jpg', 150, 86);

header('Content-type: image/jpeg');
imagejpeg($newThumb);

Link to comment
https://forums.phpfreaks.com/topic/205789-help-creating-jpg/#findComment-1077225
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.