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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.