Forum Moderators: coopster

Message Too Old, No Replies

Reduce image to do not run distortions

With PHP

         

romzinho2k7

7:03 am on May 22, 2006 (gmt 0)

10+ Year Member



Hey friends, all right?

I am trying to do a script that It reduces the image to be smaller, but proportional to the original size, to do not occur distortions. I did some attempts. I am developing a blog system and because of this, it would like that the image stayed until a size, in case pass, would be It reduces. I did some tests to do not distort layout, Put a maximum size of 500 x600. In case pass of this, he changes the proportional size

I need that it stays of the following way:

IF 800X600 = 500X375
IF 1024X768 = 500X375
IF 700X700 = 500X500
IF 780X730 = 500X468
IF 640X480 = 500X375
IF 300X480 = 300X480
IF 300X520 = 300X520

These just were examples, because if people can send other sizes

It wanted a fotolog.net style thing

That was only some tests to do not distort layout. Script follows below:

<?php

set_time_limit(0);

$image = "001.jpg";
$perc = 60;

$file_mini = explode('.', $image);
$file_mini = $file_mini[0]."_mini.jpg";

$img_size = imagecreatefromjpeg($image);
$image_x = imagesx($img_size);
$image_y = imagesy($img_size);

$x = floor($image_x * $perc/100);
$y = floor($image_y * $perc/100);

$img_final = imagecreatetruecolor($x,$y);

imagecopyresampled($img_final, $img_size, 0, 0, 0, 0, $x+1, $y+1, $image_x , $image_y);

imagejpeg($img_final, $file_mini);

imagedestroy($img_size);
imagedestroy($img_final);
?>

<html>
<head>
<title>Mini</title>
</head>
<body>
<p align="center">
<img src="<?php echo $image;?>">
&nbsp;&nbsp;&nbsp;&nbsp;
<img src="<?php echo $file_mini;?>">
</p>
<p align="center">
<b>Image Normal</b>: <?php echo $image." (".$image_x." x ".$image_y.")";?><br>
<b>Mini Created</b>: <?php echo $file_mini." (".$x." x ".$y.")";?>
</p>
</body>
</html>

Sorry my bad english... I am learning

Thanks

siMKin

3:04 pm on May 22, 2006 (gmt 0)

10+ Year Member



I'm not sure what the problem is exactly, but ...

I need that it stays of the following way:

IF 800X600 = 500X375
IF 1024X768 = 500X375
IF 700X700 = 500X500
IF 780X730 = 500X468
IF 640X480 = 500X375
IF 300X480 = 300X480
IF 300X520 = 300X520

The best is to generate a 'rule'. Otherwise you'll spend a LOT of time defining IFs for every possible resolution.

Apparently the rule is: the Width may not be larger then 500. The height has no maximum

What you do is this:

<?php 
define("MAX_WIDTH", 500);
if ($image_x > MAX_WIDTH)
{
$resizeFactor = (MAX_WIDTH / $image_x)
$x = $image_x * $resizeFactor;
$y = $image_y * $resizeFactor;
}
else
{
$x = $image_x;
$y = $image_y;
}

// $x == new width
// $y == new height
?>

romzinho2k7

11:39 pm on May 22, 2006 (gmt 0)

10+ Year Member



Thank you very much. It worked correctly.