/** * Attach a image to chating * * @return Response */ public function attach() { $chatId = (int) Input::get('chat_id'); if ((int) $chatId > 0 && !is_null($chat = Chat::find((int) $chatId)) && $chat->hasMember(Auth::user()->id)) { if (Input::hasFile('image')) { $file = Input::file('image'); if (in_array(strtolower($file->guessExtension()), array('png', 'gif', 'jpeg', 'jpg'))) { $attachment = new MessageAttachment(); $attachment->chat_id = $chat->id; $attachment->origin = ''; $attachment->thumb = ''; $attachment->save(); $this->log('Init images'); $this->log(memory_get_usage(true)); $origin = new ImageUtil($file); $attachment->origin = $this->upload($attachment->id, 'origin', $origin->getImage()); $attachment->gallery = $this->upload($attachment->id, 'gallery', $origin->resize2('gallery')->getImage()); $origin->getImage()->destroy(); $origin = null; $this->log('Gallery and origin images should be destroyed'); $this->log(memory_get_usage(true)); $this->log('Init thumbnail'); $thumb = (new ImageUtil($file))->resize2('thumbnail'); $this->log(memory_get_usage(true)); $this->log('Uploading'); $attachment->thumb = $this->upload($attachment->id, 'thumb', $thumb->getImage()); $this->log(memory_get_usage(true)); $attachment->width = $thumb->getWidth(); $attachment->height = $thumb->getHeight(); $thumb->getImage()->destroy(); $thumb = null; $this->log('Thumbnail destroyed'); $this->log(memory_get_usage(true)); $attachment->save(); $domain = 'http' . (Request::server('HTTPS') ? '' : 's') . '://' . Request::server('HTTP_HOST'); return $this->respond(array('id' => (int) $attachment->id, 'thumb' => $domain . '/api/messages/attach/thumb/' . $attachment->id, 'origin' => $domain . '/api/messages/attach/gallery/' . $attachment->id, 'width' => $attachment->width, 'height' => $attachment->height)); } else { return $this->respondWithError('Image has unsupported extension (sent file extension' . strtolower($file->guessExtension()) . ')'); } } else { return $this->respondWithError('Image isn\'t found'); } } else { return $this->respondWithError('Chat isn\'t found'); } }