Jump to content

resize a jpg


dadamssg

Recommended Posts

I have a script that resizes images the the user uploads...it works good, except for jpg's and some gif's. It doesn't capture the size of the picture and i have no idea why! i have this to recognize which type of image it is and then creates an image from that and then gets the dimensions. But it doesn't work for jpgs and gifs.

if(preg_match('/[.](jpg)$/', $newname)) {
	$im = imagecreatefromjpeg($path_to_image_directory . $newname);
} else if (preg_match('/[.](gif)$/', $filename)) {
	$im = imagecreatefromgif($path_to_image_directory . $newname);
} else if (preg_match('/[.](png)$/', $filename)) {
	$im = imagecreatefrompng($path_to_image_directory . $newname);
}

$final_width_of_image = 200;
    $path_to_thumbs_directory = 'images/thumbs/';


$ox = imagesx($im);
$oy = imagesy($im);
echo $ox."<br>";
echo $oy."<br>";
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));

$nm = imagecreatetruecolor($nx, $ny);

 

i echo the dimensions to see what they are and it doesn't echo out anything for certain images. Works great for png's as far as i know.

 

anybody know of anything i can do to resize an uploaded image?

Link to comment
https://forums.phpfreaks.com/topic/166897-resize-a-jpg/
Share on other sites

Regarding recognizing and getting image dimensions, this script I wrote seemed to work on various images:

<?php
$files = glob('./imgs/{*.gif,*.jpg,*.png}', GLOB_BRACE);
  
$allowed_types = array( "gif" => 1,
                     "jpeg" => 2,
                     "png" => 3);

echo '<pre>';
foreach($files as $f){
  $type = exif_imagetype($f);
  
  if( !in_array( $type , $allowed_types ) ) {
    //trigger_error('Filetype not supported.', E_USER_ERROR);
    echo 'Unsupported' . "\n";
  }else{
  
    print_r(getimagesize($f));
  
  }
  echo "\n";

}
echo '</pre>';
?>

 

Array
(
    [0] => 42
    [1] => 44
    [2] => 1
    [3] => width="42" height="44"
    [bits] => 8
    [channels] => 3
    [mime] => image/gif
)

Array
(
    [0] => 80
    [1] => 80
    [2] => 2
    [3] => width="80" height="80"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
Array
(
    [0] => 80
    [1] => 80
    [2] => 3
    [3] => width="80" height="80"
    [bits] => 8
    [mime] => image/png
)

 

Your "imagecreatefrom____" section could be cleaned up a bit using a switch statement, the argument being the $type above.

Link to comment
https://forums.phpfreaks.com/topic/166897-resize-a-jpg/#findComment-879985
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.