function cropImage($original, $destination, $cropposition, $cropsize, $resizeto, $quality = 0, $watermarkArray = array(), $bgcolor = "FFFFFF", $forceJPG = false) { if ($quality == 0) { $quality = CONS_JPGQUALITY; } $thumbext = explode(".", $destination); $thumbext = array_pop($thumbext); if ($thumbext == 'jpg' || $thumbext == 'png' || $thumbext == 'gif') { // removes extension $destination = explode(".", $destination); array_pop($destination); $destination = implode(".", $destination); } $ih = @getimagesize($original); if ($ih[2] == IMAGETYPE_PNG) { // png $miniatura_id = imagecreatefrompng($original); } else { if ($ih[2] == IMAGETYPE_GIF) { // gif $miniatura_id = imagecreatefromgif($original); } else { // jpg or bmp $miniatura_id = imagecreatefromjpeg($original); } } // create image on the size of the final crop size $img_dest = imagecreatetruecolor($cropsize[0], $cropsize[1]); // fill the image (jpg) or enable alpha blending (png) if ($ih[2] != IMAGETYPE_PNG || $forceJPG) { $Hbgcolor = imagecolorallocate($img_dest, hexdec(substr($bgcolor, 0, 2)), hexdec(substr($bgcolor, 2, 2)), hexdec(substr($bgcolor, 4, 2))); // forces a white bg on thumbs imagefilledrectangle($img_dest, 0, 0, $cropsize[0], $cropsize[1], $Hbgcolor); } else { imagealphablending($im_dest, $ih[2] == IMAGETYPE_JPEG || $ih[2] == IMAGETYPE_BMP || count($watermarkArray) == 0); } // crop/resample imagecopyresampled($img_dest, $miniatura_id, 0, 0, $cropposition[0], $cropposition[1], $cropsize[0], $cropsize[1], $cropsize[0], $cropsize[1]); // no need for the original image now imagedestroy($miniatura_id); // saves the destination image if ($ih[2] == IMAGETYPE_PNG && !$forceJPG) { $destination .= ".png"; @imagepng($img_dest, $destination); } else { $destination .= ".jpg"; @imagejpeg($img_dest, $destination, $quality); } // no need for destination image imagedestroy($img_dest); // guarantees access $temp = umask(0); chmod($destination, 0775); umask($temp); // performes conditional reduction w/ watermark (ok if not 2=error on resizeImageCond) return resizeImageCond($destination, $resizeto[0], $resizeto[1], $watermarkArray) != 2; }
function prepareUpload($name, $kA, &$data) { # Returns the same errCode from storefile, plus 8 (unable to create thumbails), 9 (near time limit), 10 (quota exceeded) # file format should be [field][ids_]#_thumbid (or 1 for no thumb) if (!isset($_FILES[$name])) { return 4; } else { if ($_FILES[$name]['error'] < 5 && $_FILES[$name]['error'] != 0) { return $_FILES[$name]['error']; } else { if (!is_file($_FILES[$name]['tmp_name'])) { return 3; // file not found } } } $isImg = isset($this->fields[$name][CONS_XML_TWEAKIMAGES]) || isset($this->fields[$name][CONS_XML_THUMBNAILS]); if ($isImg) { # prepare thumbnail handling variables, as well conditional thumbs if (isset($this->fields[$name][CONS_XML_CONDTHUMBNAILS])) { // /> 'a' 'b' // / t1 t2 t1 t2 // field:[ | ;][ | ;] preg_match("@ENUM \\(([^)]*)\\).*@", $this->fields[$this->fields[$name][CONS_XML_CONDTHUMBNAILS][0]][CONS_XML_SQL], $regs); $thumbsettings = explode(";", $this->fields[$name][CONS_XML_CONDTHUMBNAILS][1]); $enums = explode(",", $regs[1]); $c = 0; $found = false; foreach ($enums as $reg) { if ($data[$this->fields[$name][CONS_XML_CONDTHUMBNAILS][0]] == str_replace("'", "", $reg)) { $thumbsettings = explode("|", $thumbsettings[$c]); $found = true; break; } $c++; } if (!$found) { $thumbsettings = isset($this->fields[$name][CONS_XML_THUMBNAILS]) ? $this->fields[$name][CONS_XML_THUMBNAILS] : explode("|", $thumbsettings[0]); } } else { $thumbsettings = isset($this->fields[$name][CONS_XML_THUMBNAILS]) ? $this->fields[$name][CONS_XML_THUMBNAILS] : array(array(0, 0)); } } # quota test $this->parent->loadDimconfig(true); if (isset($this->parent->dimconfig['_usedquota']) && isset($this->parent->dimconfig['quota']) && $this->parent->dimconfig['quota'] > 0) { if ($this->parent->dimconfig['_usedquota'] > $this->parent->dimconfig['quota']) { return 10; } # quota exceeded } # prepares path $path = CONS_FMANAGER . $this->name . "/"; if (!is_dir($path)) { makeDirs($path); } if (isset($this->fields[$name][CONS_XML_FILEPATH])) { # custom path $path .= $this->fields[$name][CONS_XML_FILEPATH]; if ($path[strlen($path) - 1] != "/") { $path .= "/"; } if (!is_dir($path)) { safe_mkdir($path); } } # prepares filename with item keys $filename = $name . "_"; foreach ($kA as $id => $value) { $filename .= $value . "_"; } # prepares filetype filter if (isset($this->fields[$name][CONS_XML_FILETYPES])) { $ftypes = "udef:" . $this->fields[$name][CONS_XML_FILETYPES]; } else { $ftypes = ""; } # prepares watermark and/or crop (for images) $WM_TODO = array(); if (isset($this->fields[$name][CONS_XML_TWEAKIMAGES])) { foreach ($this->fields[$name][CONS_XML_TWEAKIMAGES] as $c => $WM) { # stamp:over(filename@x,y)[r] # [r] not implemented yet # stamp:under(filename@x,y)[r] # croptofit:top bottom left right # might have multiple with + separator $TODO = array(); $WM = explode("+", $WM); foreach ($WM as $thisWM) { $concept = explode(":", $thisWM); if ($concept[0] == "stamp") { $thisTODO = array(); $stamptype = explode("(", $concept[1]); // ...(...@x,y)R $parameters = explode(")", $stamptype[1]); // ...@x,y)R $stamptype = $stamptype[0]; $thisTODO['isBack'] = $stamptype == "under"; $resample = isset($parameters[1]) && $parameters[1] == "r"; $parameters = $parameters[0]; $parameters = explode("@", $parameters); // ...@x,y $parameters[1] = explode(",", $parameters[1]); // x,y $thisTODO['position'] = $parameters[1]; $thisTODO['filename'] = CONS_PATH_PAGES . $_SESSION['CODE'] . "/files/" . $parameters[0]; if ($resample) { $thisTODO['resample'] = explode(",", $thumbsettings[$c]); } $TODO[] = $thisTODO; } else { if ($concept[0] == "croptofit") { $TODO[] = "C" . (isset($concept[1]) ? $concept[1] : ''); } } } $WM_TODO[$c] = $TODO; } } # perform upload and possible thumbnails if the file was uploaded $errCode = 4; # supose no upload if (isset($_FILES[$name])) { # initial size check (no need to even try if it's bigger) $mfs = isset($this->fields[$name][CONS_XML_FILEMAXSIZE]) ? $this->fields[$name][CONS_XML_FILEMAXSIZE] : 0; if ($mfs > 0 && !$isImg) { if (filesize($_FILES[$name]['tmp_name']) > $mfs) { if (!isset($_FILES[$name]['virtual'])) { @unlink($_FILES[$name]['tmp_name']); } return 2; # file larger than allowed by field and is not an image (thus, we cannot reduce) } } # perform upload $thisFilename = $path . $filename . "1"; $errCode = storeFile($_FILES[$name], $thisFilename, $ftypes); # <----------------- upload happens here //$errCode = storeFile($_FILES[$name],$thisFilename,$ftypes,true); # <----------------- use this (note the true) for full debug $arquivo = explode(".", $thisFilename); $ext = strtolower(array_pop($arquivo)); # <-- ext for the file $arquivo = implode(".", $arquivo); # if ok and is an image, check thumbnails if ($errCode == 0 && $isImg) { # delete other images (could have uploaded a different image type than before on edit) $exts = array("jpg", "gif", "swf", "png", "jpeg", "ico", "bmp"); foreach ($exts as $x => $sext) { if ($sext != $ext && is_file($arquivo . "." . $sext)) { @unlink($arquivo . "." . $sext); } } # if this is not an JPG image, and it's larger then mfs, won't work at all. Abort if ($mfs > 0 && filesize($thisFilename) > $mfs && $ext != 'jpg') { if (!isset($_FILES[$name]['virtual'])) { @unlink($thisFilename); } return 2; # file larger than allowed by field and is not a resizable image } if ($ext == "swf") { // might have tweakimages (isImg) but accept flash #check if the dimensions of the flash are too big $dim = explode(",", $thumbsettings[0]); $h = getimagesize($thisFilename); if ($h[2] != IMAGETYPE_SWF && $h[2] != IMAGETYPE_SWC) { die; return 7; // swf but not a swf!? } else { if ($h[0] > $dim[0] || $h[1] > $dim[1]) { return 12; // too big } } } else { # $thisFilename has the image untreated. # Create all thumbnails, then treat the main image: $thumbVersions = count($thumbsettings); if ($thumbVersions > 1) { # has other versions/thumbnails, work these first if (!is_dir($path . "t/")) { makeDirs($path . "t/"); } for ($tb = 1; $tb < $thumbVersions; $tb++) { # for all thumbs ... if ($this->parent->nearTimeLimit()) { return 9; } $dim = explode(",", $thumbsettings[$tb]); # remember, the array start at 0, not 1 $thisFilenameT = $path . "t/" . $filename . ($tb + 1); if (!resizeImage($thisFilename, $thisFilenameT, $dim[0], isset($dim[1]) ? $dim[1] : 0, 0, isset($WM_TODO[$tb]) ? $WM_TODO[$tb] : array()) == 2) { # error! @unlink($thisFilename); return 8; // whoever called this function should also perform cleanup } } #for each thumb } # done, process main image $dim = explode(",", $thumbsettings[0]); if (resizeImageCond($thisFilename, $dim[0], isset($dim[1]) ? $dim[1] : 0, isset($WM_TODO[0]) ? $WM_TODO[0] : array()) == 2) { @unlink($thisFilename); return 8; // whoever called this function should also perform cleanup } # check mfs clearstatcache(); # resize could have changed file size if ($mfs > 0 && filesize($thisFilename) > $mfs) { if ($ext == 'jpg') { $miniatura_id = imagecreatefromjpeg($thisFilename); imagejpeg($miniatura_id, $thisFilename, 50); // reduce step2 imagedestroy($miniatura_id); clearstatcache(); if (filesize($thisFilename) > $mfs) { unlink($thisFilename); return 2; // unable to reduce more } } else { # can't reduce quality on non-jpg unlink($thisFilename); return 2; // unable to reduce more } } } } } # upload + image handling ok? if ($errCode == 0) { $this->parent->dimconfig['_usedquota'] += filesize($thisFilename); # simple quota controler, note this is not counting thumbs $this->parent->saveConfig(); } return $errCode; }
$dim = array(0, 0); } else { $dim = explode(",", $dimconfigMD[$name][CONS_XML_THUMBNAILS][0]); } $thisFilenameT = $path . "t/" . $filename . ($tb + 1); if (!resizeImage($FirstfileName, $thisFilenameT, $dim[0], isset($dim[1]) ? $dim[1] : 0, 0, isset($WM_TODO[$tb]) ? $WM_TODO[$tb] : array()) == 2) { # error! $core->errorControl->raise(208, $name, 'dincomfig'); break; } } #for each thumb } # Checks original filesize $dim = explode(",", $dimconfigMD[$name][CONS_XML_THUMBNAILS][0]); if (resizeImageCond($FirstfileName, $dim[0], isset($dim[1]) ? $dim[1] : 0, isset($WM_TODO[0]) ? $WM_TODO[0] : array()) == 2) { $core->errorControl->raise(206, $name, 'dincomfig'); } } } break; default: if (isset($_POST[$name])) { $core->dimconfig[$name] = trim($_POST[$name]); } } } } $core->log[] = "ok"; $core->saveConfig(); $core->cacheControl->dumpTemplateCaches();