Code: Select all
<?php
$origimg = $_FILES['pix']['tmp_name'];
$imagedata = getimagesize($origimg);
$maxw = 204;
$maxh = 250;
$rand = rand(1,100);
$pixname = 'pix/'.$_GET['id'].'_'.$rand.'.jpg';
if(($maxw != $imagedata[0]) && ($maxh != $imagedata[1])) {
if(($maxw < $imagedata[0]) || ($maxh < $imagedata[1])) {
if($imagedata[0] > $imagedata[1]) {
$newpercent = $imagedata[0]/$maxh;
$newimgw = $imagedata[0]/$newpercent;
$newimgh = $imagedata[1]/$newpercent;
$newimg = imagecreatetruecolor($newimgw, $newimgh);
$oldimg = imagecreatefromjpeg($origimg);
imagecopyresampled($newimg, $oldimg, 0, 0, 0, 0, $newimgw, $newimgh, $imagedata[0], $imagedata[1]);
imagejpeg($newimg, $pixname, 100);
$marginw = ($newimgw-$maxw)/2;
$marginh = 0;
} else {
$newpercent = $imagedata[0]/$maxw;
$newimgw = $imagedata[0]/$newpercent;
$newimgh = $imagedata[1]/$newpercent;
$newimg = imagecreatetruecolor($newimgw, $newimgh);
$oldimg = imagecreatefromjpeg($origimg);
imagecopyresampled($newimg, $oldimg, 0, 0, 0, 0, $newimgw, $newimgh, $imagedata[0], $imagedata[1]);
imagejpeg($newimg, $pixname, 100);
$marginw = 0;
$marginh = 0;
}
} else {
$newimg = imagecreatetruecolor($imagedata[0], $imagedata[1]);
$oldimg = imagecreatefromjpeg($origimg);
imagecopyresampled($newimg, $oldimg, 0, 0, 0, 0, $imagedata[0], $imagedata[1], $imagedata[0], $imagedata[1]);
imagejpeg($newimg, $pixname, 100);
$marginw = ($maxw-$imagedata[0])/2;
$marginh = 0;
}
} else {
$newimg = imagecreatetruecolor($maxw, $maxh);
$oldimg = imagecreatefromjpeg($origimg);
imagecopyresampled($newimg, $oldimg, 0, 0, 0, 0, $maxw, $maxh, $maxw, $maxh);
imagejpeg($newimg, $pixname, 100);
$marginw = 0;
$marginh = 0;
}
if($marginw != 0) {
$marginw1 = round($marginw).'px';
} elseif($marginh != 0) {
$marginh1 = round($marginh).'px';
} else {
$marginw1 = round($marginw);
$marginh1 = round($marginh);
}
$margin = $marginh1.','.$marginw1;
?>