Jump to content

img resolutions


ki

Recommended Posts

go nuts

[code]
<?php
$im=imagecreatefromstring(file_get_contents('some_image.jpg'));
$width=640; //max width -- change as you like
$height=480; //max height -- change as you like
if(imagesx($im)>imagesy($im)) { // if the image is wider than it is tall (landscape orentation)
if(imagesx($im)<=$width) { // is this image the right size already?
$newim=imagecreatetruecolor(imagesx($im),imagesy($im));
imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
} else { // nope, still too big.
$newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$width));
imagecopyresampled($newim,$im,0,0,0,0,$width,((imagesy($im)/imagesx($im))*$width),imagesx($im),imagesy($im));
}
} else { // if the image is square or taller than it is wide (square or portrait orentation)
if(imagesy($im)<=$height) { // is this image the right size already?
$newim=imagecreatetruecolor(imagesx($im),imagesy($im));
imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im));
} else { // nope, still too big
$newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$height),$height);
imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$height),$height,imagesx($im),imagesy($im));
}
}
header("Content-type: image/jpeg"); //if displaying, you'll need to tell the browser what this is.
imagejpeg($newim,"",90); // as written, this displays the new image. Add a filename inside the quotes to save as instead.
?>
[/code]

word of warning -- one or two every now and then should be fine. Doing this over and over to bunches of images at the same time will clock your CPU and can really slow down the server.
Link to comment
https://forums.phpfreaks.com/topic/32934-img-resolutions/#findComment-153351
Share on other sites

replace the first line with
[code=php:0]$im=imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name']));[/code]


comment the "header(Content-type...." line (second to last line in previous code)
then set a filename to save the file someplace

or inject it in your database like such
[code]<?php
//sql injection
$filename="/tmp/tempimage".rand(0,10000).".jpg";
//header("Content-type: image/jpeg");
imagejpeg($newim, $filename, 90);
$query="INSERT INTO `table` (`thumbnail`) VALUES (".mysql_real_escape_string(file_get_contents($filename))."')";
mysql_query($query);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32934-img-resolutions/#findComment-153365
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.