if (!empty($_FILES['upload']['tmp_name'])) { if (!file_exists($_FILES['upload']['tmp_name'])) { throw new Exception('Upload failed. PHP upload error: ' . intval($_FILES['upload']['error'])); } $filearray = $_FILES['upload']; $data['org_file_info'] = pathinfo($_FILES['upload']['name']); $filesize = filesize($_FILES['upload']['tmp_name']); $fileContents = file_get_contents($_FILES['upload']['tmp_name']); $filename = $_FILES['upload']['tmp_name']; $crop['org_file_info'] = pathinfo($_FILES['upload']['name']); } elseif (!empty($vbulletin->GPC['avatarurl'])) { //Make a local copy require_once DIR . '/includes/class_upload.php'; $upload = new vB_Upload_Image($vbulletin); $upload->image = vB_Image::instance(); $upload->path = vB_Utilities::getTmpDir(); $filename = $upload->process_upload($vbulletin->GPC['avatarurl']); } if ($filename) { vB_Library::instance('user')->uploadAvatar($filename, $crop, $userinfo['userid']); } else { print_stop_message2('upload_file_failed'); } } else { // not using an avatar $vbulletin->GPC['avatarid'] = 0; $userpic = new vB_Datamanager_Userpic_Avatar($vbulletin, vB_DataManager_Constants::ERRTYPE_CP); $userpic->condition = array(array('field' => 'userid', 'value' => $userinfo['userid'], 'operator' => vB_dB_Query::OPERATOR_EQ)); $userpic->delete(); } print_stop_message2('saved_avatar_successfully', 'user', array('do' => 'edit', 'u' => $vbulletin->GPC['userid']));
public function cropImg($imgInfo, $maxwidth = 100, $maxheight = 100, $forceResize = false) { $thumbnail = array('filedata' => '', 'filesize' => 0, 'dateline' => 0, 'imageerror' => ''); $execute = ''; $filename = $imgInfo['filename']; $fh = fopen($filename, 'w'); fwrite($fh, $imgInfo['filedata']); fclose($fh); if ($this->isValidThumbnailExtension($imgInfo['extension'])) { $thumbnail['source_width'] = $width = $imgInfo['width']; $thumbnail['source_height'] = $height = $imgInfo['height']; if ($forceResize or $imgInfo['width'] >= $maxwidth or $imgInfo['height'] >= $maxheight) { $xratio = $maxwidth == 0 ? 1 : $width / $maxwidth; $yratio = $maxheight == 0 ? 1 : $height / $maxheight; if ($xratio > $yratio) { $new_width = round($width / $xratio); $new_height = round($height / $xratio); } else { $new_width = round($width / $yratio); $new_height = round($height / $yratio); } // We could also use vB_Utilities::getTmpFileName() here. $tempdir = vB_Utilities::getTmpDir(); $time = time(); $tmpFileNew = $tempdir . DIRECTORY_SEPARATOR . $time . '-0.' . $imgInfo['extension']; $geometry1 = $width . "x" . $height; $geometry2 = $new_width . "x" . $new_height; $offset = $imgInfo['x1'] . "+" . $imgInfo['y1']; $execute = $this->convertpath . " {$filename} -crop {$geometry1}+{$offset} +repage -resize {$geometry2} {$tmpFileNew}"; exec($execute); if (file_exists($tmpFileNew)) { if ($imageinfo = $this->fetchImageInfo($tmpFileNew)) { $thumbnail['width'] = $imageinfo[0]; $thumbnail['height'] = $imageinfo[1]; } $extension = strtolower(file_extension($filename)); $thumbnail['filename'] = preg_replace('#' . preg_quote(file_extension($filename), '#') . '$#', 'jpg', $filename); $thumbnail['filesize'] = filesize($tmpFileNew); $thumbnail['dateline'] = vB::getRequest()->getTimeNow(); $thumbnail['filedata'] = file_get_contents($tmpFileNew); } else { throw new vB_Exception_Api('thumbnail_nogetimagesize'); } @unlink($tmpFileNew); } else { // image is a thumbnail size already if ($imgInfo['width'] > 0 and $imgInfo['height'] > 0) { $thumbnail['filedata'] = @file_get_contents($filename); $thumbnail['width'] = $imgInfo['width']; $thumbnail['height'] = $imgInfo['height']; } else { throw new vB_Exception_Api('thumbnail_nogetimagesize'); } } } else { throw new vB_Exception_Api('thumbnail_nosupport'); } if (!empty($thumbnail['filedata'])) { $thumbnail['filesize'] = strlen($thumbnail['filedata']); $thumbnail['dateline'] = vB::getRequest()->getTimeNow(); } return $thumbnail; }