Jump to content

creating two images from one source?


Harley1979

Recommended Posts

Hope someone can clrify this for me, I have a script that is getting an image from a form, and then creating a thumbnail as well as resizing the image.

 

The thumbnail works fine, the main image saves fine but for some reason is still the original size.

 

I am wondering if you can use 'imagecopyresampled' more than once on a page as this seems to be the line that fails, although no errors messages are apparent.

 

Here is my code

 

<?php

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

$section = $_REQUEST['section'];

if($section == "event"){
$event = trim($_REQUEST['event']);
if(empty($event)){
	$eventErrMsg = "You must enter an event";
} else {
	mysql_query("INSERT INTO event (event) VALUES ('$event')");
	$eventConfirmation = "Event has been successfully added!";
}
}

if ($section == "image"){

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

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

$max_size = 8388608;

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

$path = "../gallery/";
$thumbPath = "../gallery/thumbs/";

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

$src = imagecreatefromjpeg($imageFile);

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

// get last image from db
$result = mysql_query("SELECT photo FROM gallery ORDER BY id desc LIMIT 1");
$row = mysql_fetch_row($result);

// modify file name

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

/////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////   create thumbnails   //////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

// set crop size
if($origWidth > $origHeight){
$biggestside = $origWidth;
} else {
$biggestside = $origHeight;
}

// crop size will be half that of the largest side
$croppercent = .15;
$cropwidth = $biggestside*$croppercent;
$cropheight = $biggestside*$croppercent;

// get top left coordinate
$cl = array("x"=>($origWidth-$cropwidth)/2, "y"=>($origHeight-$cropheight)/2);

// create thumbnail
$thumbsize = 75;

$thumb = imagecreatetruecolor($thumbsize, $thumbsize);
imagecopyresampled($thumb, $src, 0, 0, $cl['x'], $cl['y'], $thumbsize, $thumbsize, $cropwidth, $cropheight);

// save to path
imagejpeg($thumb, $thumbPath.$newName, 100);

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

// if our image is not the desired dimension, resample to default dimensions		
if($origWidth < $origHeight && $origWidth > 450){
$newHeight = 530;
$newWidth = $origWidth * ($newHeight/$origHeight);

} elseif($origWidth > $origHeight && $origHeight > 530){
$newWidth = 550;
$newHeight = $origHeight * ($newWidth/$origWidth);
}

$mainImage = imagecreatetruecolor($newWidth, $newHeight);

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

imagejpeg($mainImage, $path.$newName, 100);

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

$insertEvent = trim($_REQUEST['event']);

mysql_query("UPDATE event SET status = 'active' WHERE id = '$insertEvent'") or die (mysql_error());

if(!isset($err) && move_uploaded_file($imageFile, $path.$newName)){
mysql_query("INSERT INTO gallery (photo, caption, event) VALUES ('$photoName', '$caption', '$event')") or die (mysql_error());
$imageMsg = "Image successfuly uploaded!";
}

} else {
$msg = "Incorrect file type";
}

}

}
error_reporting(E_ALL);
?>
<!-- event form -->
<form action="<?php $_SERVER['PHP_SELF'] ?>?section=event" method="post">
<fieldset>
<legend>Add new event<?php if(isset($eventConfirmation)){ print " | <span class='warning'>".$eventConfirmation."</span>";} ?></legend>
<label for="event"><p>Event name <?php if(isset($eventErrMsg)){echo '<span class="warning">'.$eventErrMsg.'</span>';} ?></p></label>
<div>
<input type="text" id="event" name="event" maxlength="200" size="40" tabindex="1" />
</div>
<div>
<input type="submit" id="submit" name="submit" class="submit" value="Add" tabindex="2" />
</div>
</fieldset>
</form>
<!-- image form -->
<form action="<?php $_SERVER['PHP_SELF'] ?>?section=image" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Upload image<?php if(isset($imageMsg)){ print " | <span class='warning'>".$imageMsg."</span>";} ?></legend>
<label for="caption"><p>Caption/Title <?php if(isset($captionErrMsg)){echo '<span class="warning">'.$captionErrMsg.'</span>';} ?></p></label>
<div><input type="text" id="caption" name="caption" maxlength="200" size="40" tabindex="1"<?php if(isset($caption)){echo " value='{$caption}'";} ?> /></div>
<label for="event"><p>Event</p></label>
<div>
<select id="event" name="event" tabindex="2">
<?php
$result = mysql_query("SELECT * FROM event ORDER BY id DESC");
while($row = mysql_fetch_assoc($result)){
$eventID = $row['id'];
$displayEvent = $row['event'];

print "<option value='";
	print $eventID;
print "'";
if(isset($event) && $event == $eventID){
	print " selected";
}
print ">";
print $displayEvent;
print "</option>\n";
}
?>
</select>
</div>
<label for="image"><p>Image</p></label>
<div>
<input type="file" id="imagefile" name="imagefile" tabindex="3" />
</div>
<div>
<input type="submit" id="submit" name="submit" class="submit" value="Upload" tabindex="4" />
</div>
</fieldset>
</form>

Link to comment
https://forums.phpfreaks.com/topic/64834-creating-two-images-from-one-source/
Share on other sites

I think your problem is here:

if($origWidth < $origHeight && $origWidth > 450){

$newHeight = 530;

$newWidth = $origWidth * ($newHeight/$origHeight);

 

} elseif($origWidth > $origHeight && $origHeight > 530){

$newWidth = 550;

$newHeight = $origHeight * ($newWidth/$origWidth);

}

 

If the uploaded image is less than 450x530 pixels, $newHeight and $newWidth are not set. Then the following image function calls puke because the size is null. Try this:

if($origWidth < $origHeight && $origWidth > 450){
$newHeight = 530;
$newWidth = $origWidth * ($newHeight/$origHeight);

} elseif($origWidth > $origHeight && $origHeight > 530){
$newWidth = 550;
$newHeight = $origHeight * ($newWidth/$origWidth);
} else {
$newWidth = $origWidth;
$newHeight = $origHeight;
}

This worked like a charm on my localhost.

 

Hope that helps.

Thanks for the tip there, I was forgetting about users uploading small images. My worry was that images may be uploaded straight from camera. I still have a problem though...

 

It is failing when uploading images larger than the specified dimensions (430 & 550). Instead of saving the resized image, it is saving the original.

 

Any ideas?

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.