Jump to content

Merging images


Harley1979

Recommended Posts

Hi,

 

Got this script below that resizes my images proportionately. For those images that are displayed portrait though I want to make them fit into a second image which is basically just a background pattern. How can I merge the two images?

 

Thanks

 

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

$caption = trim($_REQUEST['caption']);
if(empty($caption)){
	$err = true;
	$titleErrMsg = "You must give a title";
}

$title = $title.".jpg";

$section = $_REQUEST['section'];

$grayscale = $_REQUEST['grayscale'];

if($section == 0){
	$err = true;
	$sectionErrMsg = "You must choose a section to upload to";
}

$max_size = 8388608;

$size = $_FILES['imagefile']['size'];
$type = $_FILES['imagefile']['type'];
$imageFile = $_FILES['imagefile']['tmp_name'];

$src = imagecreatefromjpeg($imageFile);
list($origWidth, $origHeight) = getimagesize($imageFile);

if($type == "image/jpeg" || $type == "image/gif"){

/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////  resize original image  ////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

$result1 = mysql_query("SELECT photo FROM gallery ORDER BY id desc LIMIT 1");
$row = mysql_fetch_row($result1);

// modify file name

if(empty($row[0])){$galleryName = 0;}
$galleryName = $row[0] + 1;
$galleryName = $galleryName.".jpg";

$result2 = mysql_query("SELECT photo FROM sitephotos ORDER BY id desc LIMIT 1");
$row = mysql_fetch_row($result2);

// modify file name

if(empty($row[0])){$sitePhotoName = 0;}
$sitePhotoName = $row[0] + 1;
$sitePhotoName = $sitePhotoName.".jpg";

$result3 = mysql_query("SELECT name FROM flyers ORDER BY id desc LIMIT 1");
$row = mysql_fetch_row($result3);

// modify file name

if(empty($row[0])){$flyerName = 0;}
$flyerName = $row[0] + 1;
$flyerName = $flyerName.".jpg";

// if our image is not the desired dimension, resample to default dimensions

// define desireable dimensions

switch ($section){
case 1:
	$heightDetermine = 352;
	$widthDetermine = 514;
	$savepath = '../gallery/';
	$title = $galleryName;
break;

case 2:
	$heightDetermine = 137;
	$widthDetermine = 200;
	$savepath = '../sitephotos/';
	$title = $sitePhotoName;
break;

case 3:
	$heightDetermine = 298;
	$widthDetermine = 210;
	$savepath = '../flyers/';
	$title = $flyerName;

}


if($origWidth <= $origHeight && $origWidth >= $widthDetermine){
	$newHeight = $heightDetermine;
	$newWidth = $origWidth * ($newHeight/$origWidth);
} elseif($origWidth >= $origHeight && $origHeight >= $heightDetermine){
	$newWidth = $widthDetermine;
	$newHeight = $origHeight * ($newWidth/$origWidth);
} else {
	$newWidth = $origWidth;
	$newHeight = $origHeight;
}

$mainImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($mainImage, $src, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);

if($grayscale == 'on'){
imagefilter($mainImage, IMG_FILTER_GRAYSCALE);}

imagejpeg($mainImage, $savepath.$title, 100);

imagedestroy($mainImage);
imagedestroy($src);

if(!isset($err)){


switch ($section){
case 1:
	$result1 = mysql_query("INSERT INTO gallery (photo, caption) VALUES ('$galleryName', '$caption')");
break;

case 2:
	$result2 = mysql_query("INSERT INTO sitephotos (photo, caption) VALUES ('$sitePhotoName', '$caption')");

case 3:
	$result3 = mysql_query("INSERT INTO flyers (name, altText) VALUES ('$flyerName', '$caption')");
	}

} else {
	$imageErrMsg = "Incorrect file type";}
}
}
error_reporting(E_ALL);

?>

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.