Skip to content

Instantly share code, notes, and snippets.

@MoreOutput
Created October 9, 2014 14:19
Show Gist options
  • Save MoreOutput/d7631254ae0d6992c5c2 to your computer and use it in GitHub Desktop.
Save MoreOutput/d7631254ae0d6992c5c2 to your computer and use it in GitHub Desktop.
CakePHP model action for re-sizing an image -- you may want to make it a formal helper. Was written years ago YMMV.
public function createImage($file, $userID, $jobID, $entryID) {
if($file) {
echo var_dump($file);
if ((($file['type'] == 'image/jpeg') || ($file['type'] == 'image/png')) && ($file['size'] < 205000)) {
$thbPath = '/data/htdocs/indieop.com/public_html/app/webroot/img/entries/'.
$userID . '_' . $jobID . '_' . $entryID . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
$fPath = '/data/htdocs/indieop.com/public_html/app/webroot/img/entries/'.
$userID . '_' . $jobID . '_' . $entryID .'_max' . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
$imgParams = getimagesize($file['tmp_name']);
$width = $imgParams[0];
$height = $imgParams[1];
$twidth = 205;
$theight = ($twidth / $width) * $height;
$thumb = imagecreatetruecolor($twidth, $theight);
$tmpImg;
if($file['type'] == 'image/jpeg') {
$tmpImg = imagecreatefromjpeg($file['tmp_name']);
}
else {
$tmpImg = imagecreatefrompng($file['tmp_name']);
}
imagecopyresampled($thumb, $tmpImg, 0, 0, 0, 0, $twidth, $theight, $width, $height);
if($file['type'] == 'image/jpeg') {
$thumb = imagejpeg($thumb, $thbPath, 100);
}
else {
$thumb = imagepng($thumb, $thbPath);
}
if (!$file["error"] > 0) {
if(move_uploaded_file($file['tmp_name'], $fPath)) {
return true;
}
else {
return false;
}
}
}
else {
return false;
}
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment